foreach ループから取得したデータを格納する空の配列 ($results) を作成しました。配列に設定した 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
)
)