如何使用PHP读取JSON属性

12
如何使用最少的循环在 PHP 中获取以下 JSON 数据中的所有 pid 和 styles 属性?
{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}

谢谢


5
这一定是某个东西的重复。 - ZJR
你尝试过什么?你想要知道什么?你在解析JSON时遇到了问题吗?还是访问数组有问题?或者是在数组迭代方面有问题? - Felix Kling
可能是PHP中的JSON解码的重复问题。 - Stefan Gehrig
4个回答

20
$str = '{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}';

$arr = json_decode($str, true);

foreach ($arr['elements'] as $element) {
    echo 'pid: ' . $element['pid'] . '<br />';
    echo 'styles: ' . $element['styles'] . '<br />';
}

4
使用PHP中的json_decode函数获取关联数组。
<?php
    $myJson = '{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}';  
    $myArray = json_decode($myJson,true);
    $myInnerArray = $myArray['elements'];
    $styles = array();
    foreach($myInnerArray as $element)
       $styles[] = $element['styles'];
    print_r($styles);

    ?>

1

PHP有很强的处理json的能力。

假设您上面发布的JSON字符串存储在PHP变量$myJSON中。

因此,我们可以轻松地将这些值的关联数组存储到$myJSONArray中:

$myJSONArray = json_decode( $myJSON, true );

所以,现在我们只需要循环:

foreach( $myJSONArray['elements'] as $arr => $key )
     echo( "A PID: " . $key['pid'] . "\n" );

Codepad上看它的实际效果。


1
$json = json_decode('{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}', true);
$elements = $json['elements'];
foreach($elements as $element){
    $pid = $element['pid'];
    $styles = $element['styles'];
    echo $pid.': '.$styles.'<br />';
}

这里放例子


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接