I'm trying to extract only the 'phone' and 'number' values from the array but can't succeed. Here is the array:
Array
(
[success] => true
[data] => Array
(
[item] => Array
(
[0] => Array
(
[Weight] => 0.20
[Number] => 56885803183
[Phone] => 999999999999
)
[1] => Array
(
[Weight] => 0.20
[Number] => 455455183
[Phone] => 956546569999
)
[2] => Array
(
[Weight] => 0.20
[Number] => 455455183
[Phone] => 956546569999
)
)
)
)
I just can't figure out how to deal with the nested numeric keys so any help would be greatly appreciated.
foreach($array as $key => $val) {...}
isn't working.
-
1loop $array['data']['item']Ravinder Reddy– Ravinder Reddy2017年12月13日 21:46:26 +00:00Commented Dec 13, 2017 at 21:46
3 Answers 3
Loop through the nested array items, then reference the associative keys to obtain the values:
foreach($your_array['data']['item'] as $item){
echo $item['Weight'];
echo '<br/>';
echo $item['Number'];
echo '<br/>';
echo $item['Phone'];
echo '<br/>------------<br/>';
}
Comments
loop $array['data']['item']
foreach( $array['data']['item'] as $value){
echo "Phone: ".$value['Phone'];
echo '<br/>';
echo "Number: ".$value['Number'];
echo '<br/>';
}
Comments
You have to acess nested array in foreach loop. The array of items which is stored at
$arrayName[`data`][`item`]
And then define a loop traversing variable like
$eachItem
in loop. And print the value of array item using association printing
$array[`index`];
The code will look like this
foreach( $arrayName['data']['item'] as $eachItem)
{
echo "<br> Phone: ".$eachItem['Phone'];
echo "<br> Number: ".$eachItem['Number'];
}