Javascript 如何在PHP中从JSON中删除密钥?
如果在数组中找到键“src”,如何从这个JSON中删除它Javascript 如何在PHP中从JSON中删除密钥?,javascript,php,arrays,json,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":"
$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"}]';
好吧,假设您的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);
好吧,假设您的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);