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

删除多维数组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.

    点赞 打赏 评论

相关推荐 更多相似问题