【问题标题】:How can I remove a key from JSON in PHP? [duplicate]如何从 PHP 中的 JSON 中删除密钥? [复制]
【发布时间】:2017-04-06 16:31:40
【问题描述】:

如果在数组中找到该 JSON 中的键“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"}]';

【问题讨论】:

    标签: javascript php arrays json


    【解决方案1】:

    假设你的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2011-10-07
      • 1970-01-01
      相关资源
      最近更新 更多