-1

我创建了一个空数组 ($results),其中存储了从 foreach 循环返回的数据。我对在数组中设置的每个 search_tags 关键字执行此操作。一切正常,除了我想将用于从 Instagram api 获取数据的标签 ($tag) 添加到多维数组中。

$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)
    }

}

我试过用array_push来做这个,但这会在数组之后添加标签,而不是在数组内部,如下所示。

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
)

有没有一种可能的方法将标签插入到多维数组中,如下所示:

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
        )
)
4

1 回答 1

1

试试这个 :

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

}
于 2013-05-30T10:52:30.607 回答