I've made a loop that goes over all the gigs I've attended, some gigs have the same songs.

Here is the result of that array: http://snippi.com/s/k80v7pe

Now I want to have an array of all the distinct song id's. So want to see all the songs that I've ever heard:

//get the songs field get_field('songs')
foreach ($postIds as $postId){ 
    array_push($songIds, get_field('songs', $postId));  

//Over here I'm trying to print all the ID's of the song
foreach ($songIds as $songId) {
    echo $songId->ID;

But I'm getting the following error:

Notice: Trying to get property 'ID' of non-object in

Is that because of that the array $songIds has an array in an array?

2 Answers 2


No, that's because you are treating an array as an object and PHP doesn't convert those types automatically. To get an individual element from an object you use:


To get an individual element from an array you use:


You can even nest arrays. So if you have $my_array, where each element is another array, you can get elementX from arrayY like this:


In your case, apparently, you have an array, which holds an array, which holds objects, amounting to:

  • I have this: foreach ($songIds as $songId) { echo $songId[0]->ID; echo "<br />"; } But it only gives the ID's of the first gig.
    – Dennis
    Nov 24, 2019 at 19:28
  • You''ll have to loop through $songid[i]
    – cjbj
    Nov 24, 2019 at 21:53
  • Sorry, don't know how to do that exactly. Do I have to do a foreach in a foreach? Some like this? foreach ($songIds as $songId) { foreach ($songId as $item) { echo $item[0]->ID; echo "<br />"; } } But replace the [0] with a counter?
    – Dennis
    Nov 25, 2019 at 8:45
  • foreach ($songIds as $songId) { foreach ($songId as $key => $value) { print_r($key[$value]); echo "<br />"; } }
    – Dennis
    Nov 25, 2019 at 8:50
  • foreach ($songIds as $songId) {for ($i = 0; $i < count($songid); $i++) { echo $songId[$i]->ID; echo "<br />"; } }
    – cjbj
    Nov 25, 2019 at 13:31

Because $songIds is an array within an array, I have to do 2 loops within each other.

The code below pushes all post id's from the different arrays in a new array $allSongs.

This way I have an array with all the id's. Some id's will be duplicates because some songs are played more than 1 time over all the gigs.

foreach ($songIds as $songId) {
    foreach ($songId as $key => $value) {
        array_push($allSongs, $value->ID);

//all songs

To retrieve only unique id's, you can use the array function array_unique to remove the duplicates from an array.

$uniqueSongs = array_unique($allSongs, SORT_REGULAR);
  • Welcome to WPSE. Simply posting code doesn't make for a complete answer. Try to include some explanation with your answers.
    – butlerblog
    Nov 26, 2019 at 12:52
  • Thank you! I will edit my post immediately.
    – Dennis
    Nov 26, 2019 at 13:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.