0

I am trying to get the 'name' value from within a 'nested array' I am not sure if thats the correct term in PHP.

   Array ( 
        [0] => Array 
        ( 
            [event] => Array 
                ( 
                    [id] => 28140972 
                    [name] => Northwich v Lincoln United FC 
                    [countryCode] => GB 
                    [timezone] => Europe/London 
                    [openDate] => 2017-03-08T19:45:00.000Z 
                ) 
                [marketCount] => 24 
        ) 
        [1] => Array 
        ( 
            [event] => Array 
                ( 
                    [id] => 28140974 
                    [name] => Viimsi MRJK v Paide Linnameeskond II 
                    [countryCode] => EE 
                    [timezone] => Europe/London 
                    [openDate] => 2017-03-08T17:00:00.000Z 
                ) 
                [marketCount] => 24 
        ) 
    }

I am trying to access the 'name' key of every item in the array, but struggling to do it. Any advice?

2 Answers 2

2
$arrayOfNames = array_map(function ($item) {
    return $item['event']['name'];
}, $yourPreviousArray);

Test case:

>>> print_r($data)
Array
(
    [0] => Array
        (
            [event] => Array
                (
                    [id] => 1
                    [name] => James
                )

        )

    [1] => Array
        (
            [event] => Array
                (
                    [id] => 2
                    [name] => Jim
                )

        )

)
=> true
>>> array_map(function($item) {
... return $item['event']['name'];
... }, $data)
=> [
     "James",
     "Jim",
   ]
>>> 
Sign up to request clarification or add additional context in comments.

4 Comments

Trying this, but its bringing back nothing for me :( thanks
I need to see all of your code then to see what's going wrong.
got it working by changing the return to echo ;) thanks!
array_map() isn't really meant to echo from inside the callback, it would probably be better to foreach over the result of it and output the results, but I guess if that works, it works.
0

You can do something like this:

foreach ($array as $item) {
    echo $item['event']['name'].'<br/>';
}

1 Comment

Thats what I have been trying (at least I know I am on the right track) thanks - but its just bringing back nothing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.