简体   繁体   English

JSON子数组-PHP解码

[英]JSON sub array - PHP decode

I have a JSON feed and within the feed, there is an array inside the feed, I can output everything above the array correctly. 我有一个JSON提要,并且提要内有一个数组,我可以正确输出数组上方的所有内容。

JSON feed output: http://pastebin.com/pxiFVm1d JSON供稿输出: http//pastebin.com/pxiFVm1d

Code used to output: 用于输出的代码:

$jsonurl = "LINK to feed";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

foreach ( $json_output->results as $results )
{
    echo "{$results->name}.<br />";
}

How do I output the photo_rerence which is part of the array (see pastebin)? 如何输出数组的photo_rerence (请参阅pastebin)?

If there's the possibility that there might be multiple photos you could do: 如果有可能有多张照片,您可以执行以下操作:

$json_output = json_decode($json);
foreach ( $json_output->results as $results )
{
    foreach($results->photos as $photo) {
        echo $photo->photo_reference;
    }
}

Or if you only ever want to grab from the first one in the array just do: 或者,如果您只想从数组中的第一个中抓取,请执行以下操作:

$json_output = json_decode($json);
foreach ( $json_output->results as $results )
{
   echo $results->photos[0]->photo_reference;
}

您可以做一个“深度选择”: $results['photos']['photo_reference'] ,或者看看phps array_walk()函数,该函数允许您以递归方式“搜索”整个数组。

Just to add to what arkascha said, if you want to use it as an array, you will need to add the second param on your json_decode: 只是要添加到arkascha所说的内容中,如果要将其用作数组,则需要在json_decode上添加第二个参数:

$json_output = json_decode($json, TRUE); 

Refer to php.net/json_decode for details. 有关详细信息,请参考php.net/json_decode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM