如何使用foreach(instagram api)PHP将数组放入数组中

时间:2014-03-13 13:56:59

标签: php arrays json api

我还没有完成代码,但我希望在名为“data”的数组中获取$pictures的所有值。因此,在阵列“数据”中存在具有特定信息的阵列图片。然后将其编码为JSON。

function jSONEncode($data){

    foreach ($data->data as $key){
        $url = $key->images->thumbnail->url;
        $longitude = $key->location->longitude;
        $latitude = $key->location->longitude;

        $pictures = array(
            "url" => $url,
            "latitude" => $longitude,
            "longitude" => $latitude,
            "name" => "bla bla"

        );
    };
}

1 个答案:

答案 0 :(得分:1)

这可能会对您有所帮助:

$json['data'] = array();
foreach ($data->data as $key){
    $url = $key->images->thumbnail->url;
    $longitude = $key->location->longitude;
    $latitude = $key->location->longitude;

    $pictures = array(
        "url" => $url,
        "latitude" => $longitude,
        "longitude" => $latitude,
        "name" => "bla bla"

    );

    $json['data'][] = $pictures;
};

echo json_encode($json);
exit;

现在,您可以使用json.data

访问javascript