doumei4964 2019-05-23 07:27
浏览 231
已采纳

在PHP中通过键删除JSON子项

My goal is to have a function that can remove a specified json child, which could also be nested inside deeper.

My function looks like this:

private function removeJsonChild(String $jsonKey, String $jsonString)
{
    $json = json_decode($jsonString, true);
    $arr_index = array();

    foreach ($json as $key => $value) {
        if (is_array($value)) {
            $json[$key] = $this->removeJsonChild($jsonKey, json_encode($value));
        }

        if ($key == $jsonKey) {
            $arr_index[] = $key;
        }
    }

    foreach ($arr_index as $i) {
        unset($json[$i]);
    }

    return json_encode($json);
}

The function would work if i wouldn't check if a $value is an array and then call the function again recursively. But there is the problem i think. In the statement where i assign the return value of the function to $json[$key]. What am i doing wrong?

EDIT: definitely forgot a json_decode. New code looks like this:

private function removeJsonChild(String $jsonKey, String $jsonString)
{
    $json = json_decode($jsonString, true);
    $arr_index = array();

    foreach ($json as $key => $value) {
        if (is_array($value)) {
            $json[$key] = json_decode($this->removeJsonChild($jsonKey, json_encode($value)));
        }

        if ($key == $jsonKey) {
            $arr_index[] = $key;
        }
    }

    foreach ($arr_index as $i) {
        unset($json[$i]);
    }

    return json_encode($json);
}

EDIT2:

The function works now, however it slightly changes the json schema.

An JSON like this:

[
  {
    "id": 1,
    "name": "oyzadsaigny647"
  }
]

now becomes this:

{
  "1": {
    "id": 1,
    "name": "oyzadsaigny647"
  }
}
  • 写回答

1条回答 默认 最新

  • dougaicha5258 2019-05-23 07:52
    关注
    private function removeJsonChild(String $jsonKey, String $jsonString) {
    
       $data = json_decode($jsonString, true);
    
       $data = $this->removeKeyFromArray($key, $data);
    
       return json_encode($data);
    
    }
    
    private function removeKeyFromArray(String $deleteKey, array $data) {
    
      unset($data[$deleteKey]); // No need to check if it exists, it just does nothing in that case
    
      foreach($data as $key => value) {
        if(is_array($value)) {
            $data[$key] = $this->removeKeyFromArray($deleteKey, $value);
        }
      }
      return $data;
    }
    

    NOTE: this will work in case of dictionaries, i.e. array with actual keys. If you have a plain array such as [1, 10, 23, 15] the unset behaviour is wrong, as pointed out by @deceze♦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧