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

如何将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
)

图片转代码服务由CSDN问答提供 功能建议

我正在尝试按位置获取照片计数但是当我遍历各个位置时输出结果为 单独的数组。 我想把它作为单个数组出来然后转换为JSON。

 &lt;?php 
 
 $ gURL =“URL”; 
 $ pullstring = file_get_contents($ gURL); 
 $ jsonish = json_decode($ pullstring,  TRUE); 
 $ feature = $ jsonish ['features']; 
 $ arrlength = count($ feature); 
 
 for($ x = 0; $ x&lt; 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'=&gt; $ ID,'name'=&gt;  ; $ Inames,'photos'=&gt; count($ instagram_array ['data'])); 
 // print_r($ instagram_var); 
}; 
 
?&gt; 
  <  / pre> 
 
 

输出:

 数组(
 [ID] =&gt; 1 
 [name] =  &gt; 1905 
 [照片] =&gt; 15 
)数组(
 [ID] =&gt; 2 
 [名称] =&gt; 15 + ria 
 [照片] =&gt; 19 
)数组 (
 [ID] =&gt; 3 
 [name] =&gt; 18th + Amendment 
 [photos] =&gt; 19 
)数组(
 [ID] =&gt; 4 
 [name] =  &gt; 1920dc 
 [photos] =&gt; 15 
)数组(
 [ID] =&gt; 5 
 [name] =&gt; 19th 
 [photos] =&gt; 19 
)数组(\  n [ID] =&gt; 6 
 [name] =&gt; 2 + Birds + 1 + Stone 
 [photos] =&gt; 6 
)数组(
 [ID] =&gt; 7 
 [name  ] =&gt; 201 + Bar 
 [photos] =&gt; 7 
)数组(
 [ID] =&gt; 8 
 [name  ] =&gt;  30 +度+休息室
 [照片] =&gt;  19 
)数组(
 [ID] =&gt; 9 
 [name] =&gt; 51st + State 
 [photos] =&gt; 7 
)数组(
 [ID] =&gt; 10 \  n [name] =&gt; 8 + x + 10 + Club 
 [photos] =&gt; 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题