0

How can I remove the key "src" from this JSON if it is found in the array?

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

The desired end result is this:

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

1 Answers1

3

Well assuming your json was correct (the above isn't as it's missing [] around it), corrected below.

$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);
Jonathan
  • 2,778
  • 13
  • 23