如何使用PHP读取json属性

时间:2011-09-02 05:34:54

标签: php json

如何从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":""}}}}

由于

4 个答案:

答案 0 :(得分:9)

$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 />';
}

答案 1 :(得分:1)

在PHP中使用json_encode函数来获取关联数组。

<?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);

    ?>

答案 2 :(得分: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上查看此操作。

答案 3 :(得分: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 />';
}

Example here