douzhou7656 2017-09-26 11:16
浏览 209
已采纳

删除多维数组PHP中的所有特定键

I would like remove all keys [Name] but the main problem is the number in the list key ([List1],[List2] etc.). The numbers at key [List] may be more but for example I gave only two.

I would like to change this because it is an old json file and in the new one it doesn't have a key, like a converter

Is there a way to go across the entire array and remove all [Name] keys?

Array(
 [Values] => 1
 [List1] => Array(
    [Product1] => Array( 
       [0] => Array(
          [Properties] => Array( 
            [Id] => 1
            [Name] => Nm1
          )
        )
        [1]=> Array(
            [Properties] => Array(  
                [Id] => 1
                [Name] => Nm1
            )
        )
    )
    [List1] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                    [Name] => Nm1
                )
            )
        )
    )
    [List2] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                    [Name] => 0
                )
            )
        )
    )
  )
  [List2] => Array(
    [Product1] => Array( 
        [0] => Array(
            [Properties] => Array( 
                [Id] => 1
                [Name] => Nm1
            )
        )
    )
    [List1] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                    [Name] => Nm1
                )
            )
        )
    )
 )
)

My goal is:

Array(
 [Values] => 1
 [List1] => Array(
    [Product1] => Array( 
       [0] => Array(
          [Properties] => Array( 
            [Id] => 1
          )
        )
        [1]=> Array(
            [Properties] => Array(  
                [Id] => 1
            )
        )
    )
    [List1] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                )
            )
        )
    )
    [List2] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                )
            )
        )
    )
  )
  [List2] => Array(
    [Product1] => Array( 
        [0] => Array(
            [Properties] => Array( 
                [Id] => 1
            )
        )
    )
    [List1] => Array(
        [Product1] => Array( 
            [0] => Array(
                [Properties] => Array( 
                    [Id] => 1
                )
            )
        )
    )
 )
)

I tried:

$ProductCount= count($array['List1']['Product1']);
for($i = 0;$i<$ProductCount;$i++){
  unset($array['List1']['Product1'][$i][Properties][Name]);
}

But I also have a key[List2] and can be [List3] etc.

  • 写回答

1条回答 默认 最新

  • dongzao4503 2017-09-26 11:43
    关注

    You can do it like this:

    function remove_key($array, $key)
    {
        foreach($array as $k => $v) {
        if(is_array($v)) {
            $array[$k] = remove_key($v, $key);
        } elseif($k == $key) {
            unset($array[$k]);
        }
        }
        return $array;
    }
    
    $array = remove_key($array, 'Name');
    

    $array is your multidimensional array and $key is the key name that you want to remove. NOTE: If the key represents an array (not a value) this method will ignore it but I assume that this is what you need. If you want to remove arrays if the key matches you need to switch the conditions order in the foreach loop.

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示