0

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.

asked Dec 13, 2017 at 21:45
1
  • 1
    loop $array['data']['item'] Commented Dec 13, 2017 at 21:46

3 Answers 3

1

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/>';
}
answered Dec 13, 2017 at 21:50
Sign up to request clarification or add additional context in comments.

Comments

1

loop $array['data']['item']

foreach( $array['data']['item'] as $value){
 echo "Phone: ".$value['Phone'];
 echo '<br/>';
 echo "Number: ".$value['Number'];
 echo '<br/>';
}
answered Dec 13, 2017 at 21:52

Comments

1

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'];
}
answered Dec 13, 2017 at 23:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.