Search code examples
phparraysobjectmultidimensional-arrayarray-push

PHP Insert key into a multidimensional array


I created an empty array ($results) in which I store the data which I get back from the foreach loop. I do this for each search_tags keyword which I set up in an array. It all works, except I want to add the tag ($tag) which has been used to get data from the Instagram api into the multidimensional array.

$search_tags = array(
    "tag1",
    "tag2",
    "tag3"
);

// create empty array
$results=array();

// Get all the data for the tags we are searching for and put it into an array
foreach ($search_tags as $tag) {
    $medias = $instagram->getTagMedia($tag);

    foreach ($medias->data as $media_obj) {
      array_push($results, $media_obj);
      array_push($results, $tag)
    }

}

I've tried doing this with array_push, but this adds the tag after array and not inside the array as you can see below.

Array
(
    [0] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
        )
    [1] => tag1
    [2] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
        )
    [3] => tag2
    [4] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
        )
    [5] => tag3
)

Is there a possible way to insert the tag into the multidimensional array like this:

Array
(
    [0] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
            [tag] => tag1
        )
    [2] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
            [tag] => tag2
        )
    [4] => stdClass Object
        (
            [attribution] => 
            [location] => 
            [filter] => 
            [created_time] => 
            [link] => 
            [type] => 
            [id] => 
            [user] => 
            [tag] => tag3
        )
)

Solution

  • Try this :

    // Get all the data for the tags we are searching for and put it into an array
    foreach ($search_tags as $tag) {
        $medias = $instagram->getTagMedia($tag);
    
        foreach ($medias->data as $media_obj) {
          $media_obj->tag = $tag;
          array_push($results, $media_obj);
        }
    
    }