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?
Related
I have an array like this:
Array
(
[0] => Array
(
[lvid] => 01-3234
[status] => 0
[totals] => 500
[tgls] => 2020-05-24
)
[1] => Array
(
[lvid] => 01-3234
[status] => 1
[totals] => 100,100
[tgls] => 2020-05-24,2020-05-25
)
)
and i hope the result like this:
Array
(
[0] => Array
(
[01-3234] => Array
(
[0] => Array
(
[2020-05-24] => 500
)
[1] => Array
(
[2020-05-24] => 100
[2020-05-25] => 100
)
)
)
)
anybody help me,pls
Make use of explode and array_combine functions
akshay#ideapad:~$ cat test.php
<?php
$input = array(
array( "lvid" => "01-3234", "status" => 0, "totals" => 500, "tgls" => "2020-05-24" ),
array( "lvid" => "01-3234", "status" => 1, "totals" => "100,100", "tgls" => "2020-05-24,2020-05-25" )
);
// your given input samples
print_r($input);
// process and create new array
$out=array();
foreach($input as $item)
{
$toadd = array_combine(
explode(",",$item["tgls"]),
explode(",", $item["totals"])
);
if(isset($out[$item['lvid']]))
{
$out[$item['lvid']][] = $toadd;
}else
{
$out[$item['lvid']] = array($toadd);
}
}
// finally expected output
print_r(array($out));
Tested on:
akshay#ideapad:~$ php --version
PHP 7.4.6 (cli) (built: May 14 2020 10:02:44) ( NTS )
Output:
akshay#ideapad:~$ php test.php
Array
(
[0] => Array
(
[lvid] => 01-3234
[status] => 0
[totals] => 500
[tgls] => 2020-05-24
)
[1] => Array
(
[lvid] => 01-3234
[status] => 1
[totals] => 100,100
[tgls] => 2020-05-24,2020-05-25
)
)
Array
(
[0] => Array
(
[01-3234] => Array
(
[0] => Array
(
[2020-05-24] => 500
)
[1] => Array
(
[2020-05-24] => 100
[2020-05-25] => 100
)
)
)
)
I have this array:
Array ( [0] => Array ( [asset] => track [path] => media/promenade_web/AUDIO/promenade-arkadiev.mp3 [file_name] => promenade-arkadiev [permission_audio_play_synchronization] => Array ( [synchronization] => Array ( [constraint] => Array ( [numberOfMeasures] => Array ( [startMeasure] => 1 [number] => 10 ) [qualityOfResource] => medium ) [requirement] => Array ( ) [condition] => Array ( ) ) [play] => Array ( [constraint] => Array ( [numberOfMeasures] => Array ( [startMeasure] => 1 [number] => 10 ) [qualityOfResource] => medium [spatial] => iso3166:CH,IT [count] => 10 [datetime] => Array ( [start] => 2017-08-16 [end] => 2017-10-20 ) [accumulated] => P30D [format] => mp3,wav ) [requirement] => Array ( ) [condition] => Array ( ) ) ) ) [1] => Array ( [asset] => track [path] => media/promenade_web/AUDIO/promenade-arkadiev_lo.mp3 [file_name] => promenade-arkadiev_lo [permission_audio_play_synchronization2] => Array ( [play] => Array ( [constraint] => Array ( [numberOfMeasures] => Array ( [startMeasure] => 1 [number] => 10 ) [qualityOfResource] => medium [spatial] => iso3166:CH,IT [count] => 10 [datetime] => Array ( [start] => 2017-08-16 [end] => 2017-10-20 ) [accumulated] => P30D [format] => mp3,wav ) [requirement] => Array ( [prepay] => Array ( [amount] => 0.99 [currency] => EUR ) ) [condition] => Array ( ) ) ) ) )
How can I find a value with foreach, regardless of the number of subarrays?
I've tried with this code, but it doesn't work:
function recursive_array_search($needle, $haystack, $currentKey = '') {
foreach($haystack as $key=>$value) {
if (is_array($value)) {
$nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
if ($nextKey) {
return $nextKey;
}
}
else if($value==$needle) {
return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey;
}
}
return false;
}
I always use following on multi dimensional array
$arr = array(array('id'=>1,'name'=>'nilesh'),array('id'=>2,'name'=>'ajay'));
echo "<pre>";
print_r($arr[array_search('nilesh', array_column($arr,'name'))]);
echo "</pre>";
I am calling a PHP file using http.post, passing a json object in the process.
I have managed to retrieve the object from within the PHP and have attached the dump below. All I now need is to retrieve 'name', 'email' and 'message' strings from the array but am finding this difficult as not used to PHP.
Connected successfully<pre>string(2467) "Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
"
<br /><br />Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
</pre>
The PHP code which retrieves the object in the first place is as follows:
$data = json_decode(file_get_contents('php://input'), TRUE);
$text = print_r($data,true);
echo "<pre>";
var_dump($text);
echo "<br /><br />";
print_r($text);
echo "</pre>";
How can I access the 'name', 'email' and 'message' strings please?
First of all your array isnt a valid one... it should be something like this:
BTW remove the $ from inside the arrays.
$newarr = Array(
"name" => Array(
"first_name" => "Alex",
"last_name" => "Gonzalez"
),
"email" => Array(),
"other_sub_array" => Array()
);
Now to get let say the first name, since it is a subarray (so an array inside another array).
echo $newarr['name']['first_name'];
// Result: Alex
Hope this helps.
UPDATE
I didn't mean not valid, it is a bad practice to use an array like that.
Your array is valid unlike the other answer supposes. I assume that you are using some framework that generates the relatively strange json you're reading in. But to the point you can get them via:
//note I used single quotes so that the dollar sign isn't evaluated to a php variable
$data['name']['$modelValue'];
$data['message']['$modelValue'];
$data['email']['$modelValue'];
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
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