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 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?