如何从PHP中删除JSON中的密钥?

时间:2017-04-06 16:31:40

标签: javascript php arrays json

如何删除密钥" src"如果在数组中找到它,可以从这个JSON中获取吗?

$json = '[{"label":"360","file":"http://aaa","src":"http://aaa"},
{"label":"480","file":"http://bbb","src":"http://bbb"},
{"label":"720","file":"http://ccc","src":"http://ccc"},
{"label":"1080","file":"http://ddd","src":"http://ddd"}]';

期望的最终结果是:

$json = '[{"label":"360","file":"http://aaa"},
{"label":"480","file":"http://bbb"},
{"label":"720","file":"http://ccc"},
{"label":"1080","file":"http://ddd"}]';

1 个答案:

答案 0 :(得分:0)

假设你的json是正确的(上面没有,因为它周围缺少[]),在下面纠正。

$json = '[{"label":"360","file":"http://aaa","src":"http://aaa"},{"label":"480","file":"http://bbb","src":"http://bbb"},{"label":"720","file":"http://ccc","src":"http://ccc"},{"label":"1080","file":"http://ddd","src":"http://ddd"}]';

$decoded = json_decode($json, true);

foreach($decoded as $id => $row) {
    unset($row[$id]['src']);
}

$json = json_encode($decoded);