Searching in a multidimensional array - php

I've an array:
Array
(
[_edit_lock] => Array
(
[0] => 1434971582:11
)
[_edit_last] => Array
(
[0] => 11
)
[_wp_page_template] => Array
(
[0] => page-templates/langenfeldDreiSpalterMitSiderbarsRL.php
)
[_wpas_done_all] => Array
(
[0] => 1
)
[hefo_before] => Array
(
[0] => 0
)
[hefo_after] => Array
(
[0] => 0
)
[sharing_disabled] => Array
(
[0] => 1
)
[spacious_page_layout] => Array
(
[0] => left_sidebar
)
[_thumbnail_id] => Array
(
[0] => 2641
)
[ort] => Array
(
[0] => langenfeld
)
)
I want to save the "ort" in a variable.
[ort] => Array
(
[0] => langenfeld
)
My code give me the values of the array but how can I save the values?
My code:
foreach ($gpc as $k){
foreach ($k as $v){
//echo $v;
}
}
I thought something like that:
$ort = $v['ort'];
But that's not working for me. Can someone help?

This code is working properly
$arr = Array
(
'_edit_lock' => Array
(
'0' => "1434971582:11",
),
'_edit_last' => Array
(
'0' => "11",
),
'_wp_page_template' => Array
(
'0' => "page-templates/langenfeldDreiSpalterMitSiderbarsRL.php",
),
'_wpas_done_all' => Array
(
'0' => "1",
),
'hefo_before' => Array
(
'0' => "0",
),
'hefo_after' => Array
(
'0' => "0",
),
'sharing_disabled' => Array
(
'0' => "1",
),
'spacious_page_layout' => Array
(
'0' => "left_sidebar",
),
'_thumbnail_id' => Array
(
'0' => "2641",
),
'ort' => Array
(
'0' => "langenfeld",
),
);
$ort = $arr["ort"];
print_r($ort);
// output
Array
(
[0] => langenfeld
)
if you directly want langenfeld
$ort = $arr['ort'][0];
//this will output - langenfeld

Related

PHP Looping over Nested Array [duplicate]

This question already has answers here:
PHP Getting Values From Nested Array
(2 answers)
Closed last year.
I have an array which looks like this:
Array
(
[response] => Array
(
[dataInfo] => Array
(
[totalRecordCount] => 362
[foundCount] => 5
[returnedCount] => 5
)
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1020
[groupAssetName] => Standard Equipment
)
[portalData] => Array
(
)
[recordId] => 823
[modId] => 1
)
[1] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1001
[groupAssetName] => Tools
)
[portalData] => Array
(
)
[recordId] => 829
[modId] => 1
)
[2] => Array
(
[fieldData] => Array
(
[groupAssetID] => 1005
[groupAssetName] => Spare Parts
)
[portalData] => Array
(
)
[recordId] => 830
[modId] => 1
)
)
)
[messages] => Array
(
[0] => Array
(
[code] => 0
[message] => OK
)
)
)
I'm trying to loop over this and extract the values for groupAssetID and groupAssetName in the data array but haven't been able to get this to work so far. I've tried:
foreach ( $records as $record) {
echo $field . ": " . $value . "<br />\r\n" ;
}
and
foreach ( $records->fieldData as $field=>$value) {
echo $field . ": " . $value . "<br />\r\n" ;
}
but none of these seem to work and I can't work out the correct syntax here.
I managed it to run with this syntax:
$records = array
(
'response' => array
(
'dataInfo' => array
(
'totalRecordCount' => 362,
'foundCount' => 5,
'returnedCount' => 5
),
'data' => array
(
0 => array
(
'fieldData' => array
(
'groupAssetID' => 1020,
'groupAssetName' => 'Standard Equipment'
),
'portalData' => array
(
),
'recordId' => 823,
'modId' => 1
),
1 => array
(
'fieldData' => array
(
'groupAssetID' => 1001,
'groupAssetName' => 'Tools'
),
'portalData' => array
(
),
'recordId' => 829,
'modId' => 1
),
2 => array
(
'fieldData' => array
(
'groupAssetID' => 1005,
'groupAssetName' => 'Spare Parts'
),
'portalData' => array
(
),
'recordId' => 830,
'modId' => 1
)
)
),
'messages' => array
(
0 => array
(
'code' => 0,
'message' => 'OK'
)
)
);
foreach ($records['response']['data'] as $key => $value) {
echo $value['fieldData']['groupAssetID'] . ": " . $value['fieldData']['groupAssetName'] . "<br />\r\n" ;
}
Output:
1020: Standard Equipment
1001: Tools
1005: Spare Parts
Is this what you need?

change a key of a single element array

I have an array tree from a database, I want to change the key of a child element in this case the second array 'eric'=>array into integer '0'=>array as follow :
0 => Array
('text' => 'paris',
'nodes' => Array
('eric' => Array
( 'text' => 'eric',
'nodes' => Array
(0 => Array
(
'text' => 'so.png',
),
),
),
),
),
there is my code :
while($d = mysqli_fetch_assoc($result)) {
if(!isset($data[$d['country']])) {
$data[$d['country']] = array(
'text' => $d['country'],
'nodes' => array()
);
}
if(!isset($data[$d['country']]['nodes'][$d['name']])) {
$data[$d['country']]['nodes'][$d['name']] = array(
'text' => $d['name'],
'nodes' => array()
);
}
array_push($data[$d['country']]['nodes'][$d['name']]['nodes'], $d['n_doc']);
}
To change all of the child keys to numeric values, you can simply just use array_values()
Live Demo
for($i = 0; $i <= count($data) -1; $i++) { # This loops through each country
$data[$i]['nodes'] = array_map(function($node) { # This preserves the parent text value
return array_values($node); # [0] => Paris, [1] => array(...)
}, $data[$i]['nodes']);
}
Output
[ ... => [ text => Paris, nodes => [ 0 => Paris, 1 => [ ... ] ] ... ] ... ]
can you change your code for this input:
Array
(
[0] => Array
(
[text] => paris
[nodes] => Array
(
[jacque] => Array
(
[text] => jacque
[nodes] => Array
(
[0] => 32.png
)
)
[anis] => Array
(
[text] => anis
[nodes] => Array
(
[0] => 5384a97ee9d6b (2).pd
)
)
)
)
[1] => Array
(
[text] => london
[nodes] => Array
(
[dodo] => Array
(
[text] => dodo
[nodes] => Array
(
[0] => 148782.svg
[1] => 333.png
)
)
[sd] => Array
(
[text] => sd
[nodes] => Array
(
[0] => 1014-favicon.ico
)
)
)
)
)

how to generate multidimensional array from array value

i'm new in php and i stuck some where actually i need to generate multidimensional array from array value.
e.g my array is like that and remember all array and value are dynamic
array(
0 => array(
0 => "college"
1 => "student"
2 => "contact"
),
1 => array(
0 => "college"
1 => "parents"
2 => "contact"
),
2 => array(
0 => "school"
1 => "parents"
2 => "contact"
),
3 => array(
0 => "school"
1 => "student"
2 => "contact"
))
and i want result like that
0 => array (
"college" => array(
"student" => array (
"contact" => array (
"address" => "address_value"
)
),
"parents" => array (
"contact" => array (
"address" => "address_value"
)
),
),
"school" => array(
"student" => array (
"contact" => array (
"address" => "address_value"
)
),
"parents" => array (
"contact" => array (
"address" => "address_value"
)
),
)),
i want to generate multidimensional array till the array value and last array has some value
can any one help me with standard way.
help will appreciated..
thanks in advance
Try this:
<?php
function group($a, $level, $previous = '') {
$b = [];
for( $i = 0, $n = count($a); $i < $n; ++$i ) {
if( $level > 0 && $a[$i][$level-1] !== $previous ) {
continue;
}
$key = $a[$i][$level];
$b[$key] = [];
if( array_key_exists($level+1, $a[$i]) ) {
$b[$key] = group($a, $level+1, $key);
}
}
return $b;
}
print_r(group($a, 0));
Output:
Array(
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
)
Using #AlivetoDie example:
Array (
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ()
)
[student] => Array (
[contact] => Array ()
)
[data] => Array (
[contact] => Array()
)
)
)

How to format an array in php [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
There is an array with the following format.
I have used array_chunk function to format like the following array.
Array (
[0] => Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (2.5%)] => 2.5000
)
)
[1] => Array
(
[0] => Array
(
[CGST (6%)] => 6.0000
)
[1] => Array
(
[SGST (6%)] => 6.0000
)
)
)
All I need my array to be displayed in the following format
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)
Help to create such format.Thanks
try combination of array_map, array_reduce with array_merge
To know more about used function check function doc on php.net
<?php
$array = array (
'0' => array
(
'0' => array
(
'SGST (2.5%)' => '2.5000'
),
'1' => array
(
'CGST (2.5%)' => '2.5000'
)
),
'1' => array
(
'0' => array
(
'CGST (6%)' => '6.0000'
),
'1' => array
(
'SGST (6%)' => '6.0000'
)
)
);
echo '<pre>';
$processed = array_map(function($a) { return array_reduce($a, 'array_merge', array()); }, $array);
print_r($processed);
Output
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)

check duplicacy in multidimensional array [duplicate]

This question already has answers here:
PHP: Check for duplicate values in a multidimensional array
(6 answers)
Closed 7 years ago.
I have an array, last two elements are identical, i just want to check duplicate exist or not.
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
[3] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
You need to use array_unique function of PHP as
$ara = Array ( Array ( 'crop' => 'CI-000001', 'type' => 'PT-000001' ), Array
(
'crop' => 'CI-000001',
'type' => 'PT-000003'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
)
);
echo "<pre>";
print_r(array_unique($ara,SORT_REGULAR));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Try the following code:
$hashes=array();
foreach ($myarray as $key=>$item) {
$hash=sha1(var_export($item, true));
if (isset($hashes($hash)) echo "$key is a duplicate of ".$hashes[$hash];
else $hashes[$hash]=$key;
}
try like this
<?php
$array = array(array('crop' => 'CI-000001','type' => 'PT-000001'), array('crop' => 'CI-000001','type' => 'PT-000003'),array('crop' => 'CI-000005','type' => 'PT-000014'),array('crop' => 'CI-000005','type' => 'PT-000014'));
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "After Remove Duplicate:".'<pre>';
print_r( $array );
echo '</pre>';
?>
Output:-
After Remove Duplicate:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Demo

Categories