dongtui2029 2012-04-18 00:10 采纳率: 0%
浏览 50
已采纳

使用php进行基本循环来构建json

I'm querying the instagram api to return json with this code:

$instagramClientID = '9110e8c268384cb79901a96e3a16f588';

$api = 'https://api.instagram.com/v1/tags/zipcar/media/recent?client_id='.$instagramClientID; //api request (edit this to reflect tags)

$response = get_curl($api); //change request path to pull different photos

So I want to decode the json

if($response){
    // Decode the response and build an array
    foreach(json_decode($response)->data as $item){
...

So now I want to reformat the contents of said array into a particular json format (geojson) the code would be roughly this:

array(
'type' => 'FeatureCollection',
'features' => array(
    array(
        'type' => 'Feature',
        'geometry' => array(
            'coordinates' => array(-94.34885, 39.35757),
            'type' => 'Point'
        ), // geometry
        'properties' => array(
            // latitude, longitude, id etc.
        ) // properties
    ), // end of first feature
    array( ... ), // etc.
) // features
)

And then use json_encode to return it all into a nice json file to cache on the server.

My question...is how to I use the code above to loop through the json? The outer structure of the array/json is static but the interior needs to change.

  • 写回答

1条回答 默认 最新

  • duanke2012 2012-04-18 04:12
    关注

    In this case it's better to build a new data structure instead of replacing the existing one inline.

    Example:

    <?php
    $instagrams = json_decode($response)->data;
    
    $features = array();
    foreach ( $instagrams as $instagram ) {
        if ( !$instagram->location ) {
            // Images aren't required to have a location and this one doesn't have one
            // Now what? 
            continue; // Skip?
        }
    
        $features[] = array(
            'type' => 'Feature',
            'geometry' => array(
                'coordinates' => array(
                    $instagram->location->longitude, 
                    $instagram->location->latitude
                ),
                'type' => 'Point'
            ),
            'properties' => array(
                'longitude' => $instagram->location->longitude,
                'latitude' => $instagram->location->latitude,
                // Don't know where title comes from
                'title' => null,
                'user' => $instagram->user->username,
                 // No idea where id comes from since instagram's id seems to belong in instagram_id
                'id' => null,
                'image' => $instagram->images->standard_resolution->url,
                // Assuming description maps to caption
                'description' => $instagram->caption ? $instagram->caption->text : null,
                'instagram_id' => $instagram->id,
            )
        );
    }
    
    $results = array(
        'type' => 'FeatureCollection',
        'features' => $features,
    );
    
    print_r($results);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?