我创建了一个空数组($results),其中存储了从foreach循环返回的数据。我对在数组中设置的每个search_tags关键字都这样做。这一切都工作,除了我想添加标签($tag),它已用于从Instagram api获取数据到多维数组。
$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
)
)
试试这个:
// 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);
}
}