2017-04-06 38 views
-1

如何从该JSON中删除键“src”,如果它在数组中找到?如何从PHP中的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"}]'; 

回答

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