duanfan9859 2015-05-18 21:14
浏览 271
已采纳

如何将for循环的输出合并到PHP中的单个数组或JSON文件中?

I'm trying to get the counts of photos by location but when I loop through various locations the output comes out as separate arrays. I would like it all to come out as a single array to then be converted to JSON.

 <?php

    $gURL= "URL";
    $pullstring = file_get_contents($gURL);
    $jsonish = json_decode($pullstring, TRUE);
    $feature= $jsonish['features'];
    $arrlength = count($feature);

    for($x = 0; $x < 10; $x++)
    {

        $Inames= str_replace(" ", "+", $feature[$x]['properties']['name']);
        $lats= $feature[$x]['geometry']['coordinates'][1];
        $longs= $feature[$x]['geometry']['coordinates'][0];
        $ID= $feature[$x]['properties']['osm_id'];
        $numba='XXXXXXXXXXXX';  

        $instagram_URL = 'https://api.instagram.com/v1/media/search?lat='.$lats.'&lng='.$longs.'&distance=10&client_id='.$numba;
        $instagram_json = file_get_contents($instagram_URL);
        $instagram_array = json_decode($instagram_json, TRUE);
        $instagram_var= array('ID'=>$ID,'name'=>$Inames,'photos'=>count($instagram_array['data']));
        //print_r ($instagram_var);
    };

    ?>

Output:

  Array (
[ID] => 1
[name] => 1905
[photos] => 15
) Array (
[ID] => 2
[name] => 15+ria
[photos] => 19
) Array (
[ID] => 3
[name] => 18th+Amendment
[photos] => 19
) Array (
[ID] => 4
[name] => 1920dc
[photos] => 15
) Array (
[ID] => 5
[name] => 19th
[photos] => 19
) Array (
[ID] => 6
[name] => 2+Birds+1+Stone
[photos] => 6
) Array (
[ID] => 7
[name] => 201+Bar
[photos] => 7
) Array (
[ID] => 8
[name] => 30+Degrees+Lounge
[photos] => 19
) Array (
[ID] => 9
[name] => 51st+State
[photos] => 7
) Array (
[ID] => 10
[name] => 8+x+10+Club
[photos] => 19
)
  • 写回答

1条回答 默认 最新

  • duannai1883 2015-05-18 21:19
    关注

    Make your $instagram_var a two-dimensional array.

    $instagram_var = Array();
    for ($...) { // your loop
         /* ... */
         $instagram_var[] = array('ID'=>$ID,'name'=>$Inames,'photos'=>count($instagram_array['data']));
    }
    

    Afterwards you can access each individual Array with $instagram_var[0], $instagram_var[1] ... and its inner values with $instagram_var[0]['ID'] etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64