【问题标题】:PHP Parsing Multidimensional JSON ArrayPHP解析多维JSON数组
【发布时间】:2012-12-30 06:17:04
【问题描述】:

我正在使用 Angel list api 来提取此数据并尝试通过此代码对其进行解析(通过尝试解析进行提取)

解析代码无法正常工作,并且由于某种原因无法检索 [id] 的值。我一直在查找 json 深度并认为这可能是问题所在,并尝试了许多不同的方法,但似乎没有任何效果。有人可以帮助从嵌套数组中提取单个值吗?

$ch = curl_init($array_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$response = json_decode($data);
print $response->{'name'};
echo "\n";

示例输出

Array
(
[0] => Array
    (
        [id] => 79840
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/79840-e351f55eea90356818e586ca3e547981-thumb_jpg.jpg?buster=1333248008
        [url] => https://angel.co/joinastartup-org
        [name] => JoinAStartup.org
        [type] => Startup
    )

[1] => Array
    (
        [id] => 121425
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/121425-7cea796d5afc33877d68c633963ec8e9-thumb_jpg.jpg?buster=1353964843
        [url] => https://angel.co/jointly
        [name] => Jointly
        [type] => Startup
    )

[2] => Array
    (
        [id] => 76554
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/76554-d227b51eac3509eeba4cced14186b782-thumb_jpg.jpg?buster=1347923043
        [url] => https://angel.co/supportlocal
        [name] => SupportLocal
        [type] => Startup
    )

[3] => Array
    (
        [id] => 30334
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/30334-219f36ea273ef199c2aaf7b034554b90-thumb_jpg.jpg?buster=1326922125
        [url] => https://angel.co/jointli
        [name] => Jointli
        [type] => Startup
    )

[4] => Array
    (
        [id] => 14278
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/14278-8b9059bb1fa7fab5813dfbade19056eb-thumb_jpg.jpg?buster=1315736993
        [url] => https://angel.co/joint
        [name] => joint
        [type] => Startup
    )

[5] => Array
    (
        [id] => 22687
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/22687-94e1104706736e7636d23ff055819b8c-thumb_jpg.jpg?buster=1337954356
        [url] => https://angel.co/join-our-talents
        [name] => Join Our Talents
        [type] => Startup
    )

[6] => Array
    (
        [id] => 45059
        [pic] => 
        [url] => https://angel.co/joint-research-centre
        [name] => Joint Research Centre
        [type] => Startup
    )

[7] => Array
    (
        [id] => 119433
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/119433-585159079dc0e8b31156e772d53914de-thumb_jpg.jpg?buster=1346721187
        [url] => https://angel.co/joinspeaker-1
        [name] => JoinSpeaker
        [type] => Startup
    )

[8] => Array
    (
        [id] => 40214
        [pic] => 
        [url] => https://angel.co/advanced-joining-technology
        [name] => Advanced Joining Technology
        [type] => Startup
    )

[9] => Array
    (
        [id] => 35338
        [pic] => https://s3.amazonaws.com/photos.angel.co/startups/i/35338-52ca618ccef71f1a67b21e2e838a4674-thumb_jpg.jpg?buster=1326845104
        [url] => https://angel.co/joingo
        [name] => Joingo
        [type] => Startup
    )

)

【问题讨论】:

  • 这里的实际问题是什么?

标签: php parsing json


【解决方案1】:

我认为问题是为什么 print 语句不起作用?首先,每行末尾都有 <br>,这不是有效的 PHP 语法,我希望这不在您的实际代码中。

其次,关于打印语句...响应是一个数组,因此您可以使用以下方式访问它:

$response[0]['name']

0 替换为您要访问的元素的索引。您甚至可以使用循环来遍历结果并获取所有名称

$len = count($response);
for ($i = 0; $i < $len; ++$i) {
    print $response[$i]['name'];
}

您也可以使用foreach 循环。

【讨论】:


  • 只是为了在 SO 上进行格式化,我的粘贴行为很奇怪。现在正在尝试实施。谢谢!
【解决方案2】:

您需要使用foreach 之类的方式遍历数组,例如

变化:

print $response->{'name'};

收件人:

foreach ($response as $r) {
    echo $r['name'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多