如何访问JSON解码的关联数组PHP中的元素

时间:2014-02-14 23:39:07

标签: php json

我是php的新手,我正在措辞如何返回JSON数据类型的关联数组?所以这是我的PHP代码:

$JSON = file_get_contents($url);

// echo the JSON (you can echo this to JavaScript to use it there)
//echo $JSON;

// You can decode it to process it in PHP
$data = json_decode($JSON,true);
var_dump($data);

以下是将数据转储到屏幕时数据的样子片段:

array(2) {
["product"] => array(1) { 
    [0]=> array(7) { 
        ["styles"]=> array(13) { 
            [0]=> array(7) { 
                ["price"]=> string(6) "$64.95" 
                ["productUrl"]=> string(46) "http://www.zappos.com/product/7515478/color/25" 
                ["percentOff"]=> string(2) "0%" 
                ["styleId"]=> string(7) "1788226" 
                ["imageUrl"]=> string(65) "http://www.zappos.com/images/z/1/7/8/8/2/2/1788226-p-DETAILED.jpg" 
                ["color"]=> string(6) "Almond" 
                ["originalPrice"]=> string(6) "$64.95" 
            }

现在我将如何访问第一个数组元素? 我试过了:

echo $data[0][0]['orignalPrice'];

它不起作用。有人可以帮忙吗?在Mac上还有一个像样的调试器吗?感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

试试$data['product'][0]['styles'][0]['originalPrice']

还建议您查看文档,了解有关arrays的更多信息。