1

我正在使用 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
    )

)
4

2 回答 2

2

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

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

$response[0]['name']

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

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

您还可以使用foreach循环。

于 2012-12-30T06:22:55.400 回答
1

您需要使用类似的东西遍历数组foreach,例如

改变:

print $response->{'name'};

到:

foreach ($response as $r) {
    echo $r['name'];
}
于 2012-12-30T06:27:08.660 回答