Question

This one is starting to get on my nerves. Still fairly new to arrays and objects.

I need to be able to pull out [id] in the numbered array, plus get access to the lonely snippet_count at the end.

I can do it if there is no top level container array using a foreach $a as $k => $v., (from an earlier SO question) but am struggling a level deeper. Thanks.

Array
(
    [snippets] => Array
    (
        [0] => stdClass Object
            (
                [id] => 123456789
            )
        [1] => stdClass Object
            (
                [id] => 123456789
            )
        [2] => stdClass Object
            (
                [id] => 123456789
            )
        //and so on
    )
    [snippet_count] => 500
)
Was it helpful?

Solution

You can iterate over just the snippets array to get the IDs

$ids = array();
foreach ($array['snippets'] as $snippet) {
   $ids[] = $snippet->id;
}

$count = $array['snippet_count'];

Is this what you're looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top