2012-01-02 165 views
0

我建立一个JSON数组如下:PHP和JSON - 访问数据

$footerSEOArray[$data['domainname']] = $data[$language]; 
echo json_encode($footerSEOArray); 

一旦传回我这样对其进行解码:

$footerLinks = json_decode($result); 

我可以打印排列如下:

print_r($footerLinks); 

,在打印时它看起来像这样:

stdClass Object 
(
[www.data1.com] => Australia 
[www.data2.com] => Hindu 
[www.data3.com] => Laos 
[www.data4.com] => Iranian 
[www.data5.com] => America 
) 

现在我需要能够打印数组的第一部分和第二部分,但我似乎无法让它在print_r()之外打印。

THX

+0

这是一个对象而不是数组。你应该学习如何访问对象属性或数组项。 – 2012-01-02 12:03:26

+2

'$ footerLinks'是一个**对象**。您可能希望将其作为数组来获取,您必须将'true'作为第二个参数传递给'json_decode'。然后你可以遍历数组。 – 2012-01-02 12:04:09

+0

您应该使用echo $ footerLinks [0] - > {'www.data1.com'}; – dkulkarni 2012-01-02 12:08:57

回答

1

你必须传递到json_decode一个额外的参数,以获得一个数组,而不是一个普通的数组。

$footerLinks = json_decode($result, true); 

参考:PHP手册json_decode

0

尝试:

 

$footerLinks = (array) $footerLinks; 
 
1

你应该第二个参数传递给json_decode功能。它的默认行为是将键值对转换为stdClass对象,但是使用此参数强制使用关联表来代替。

所以:

$footerLinks = json_decode($result, true); 
0

正如都说$ footerlinks是一个对象,如果你想使用它,就像你解码一个这样的数组:

$footerLinks = json_decode($result,true); 

现在,$ footLinks变成了一个关联数组。