2012-12-30 130 views
1

我使用的是天使列表API将这些数据并尝试通过该代码来解析它(通过试图解析拉)PHP解析多维JSON数组

解析代码工作不正常,也不会检索由于某种原因[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 
    ) 

) 
+2

什么是这里的实际问题? – quickshiftin

+0

编辑原始 – rich

回答

2

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

第二,关于打印语句...响应是一个数组,所以你访问它使用:

$response[0]['name'] 

替换0与您要访问的元素的索引。你甚至可以使用一个循环来遍历结果,并得到所有的名字

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

你也可以使用一个foreach循环。

+0


只是为了格式化工作,我的贴子很奇怪。试图现在执行。谢谢! – rich

1

您可以通过使用类似foreach阵列需要循环,并例子是

变化:

print $response->{'name'}; 

要:

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