php - 如何使用 PHP 读取 json 属性

标签 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":""}}}}

谢谢

最佳答案

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

关于php - 如何使用 PHP 读取 json 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279585/

相关文章:

php - 检测用户点击按钮的次数

PHP include/require 内部函数

json - 破坏寻呼机中的 JSON "less"

c# - 将 JSON 数据发布到 ASP.NET MVC

php - 如何通过 php 从 mysql 数据库返回多行/所有行并制作 json?

php - 将jquery mobile和php mysql转为apk文件

php - 如何对文本文件的列求和

java - 教程第 6 部分中出现错误 : JSON Parsing and Android ListView Design

php - 根据键的值将数组拆分成更小的数组?

javascript - 在 Node.js 中使用 JavaScript 从 JSON 对象获取和设置文本内容