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.

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/>';

}
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/>';
}

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'];
}

Comments

Your Answer

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.