【发布时间】:2013-05-30 10:48:44
【问题描述】:
我创建了一个空数组 ($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
)
)
【问题讨论】:
标签: php arrays object multidimensional-array array-push