从php中的二维数组中获取密钥

时间:2018-02-27 11:05:59

标签: php arrays

我有像这样的二维数组

 $data = Array
  (
   [1] => Array
    (
        [type] => Combine
        [aggregator] => all
        [value] => 1
        [new_child] => 
    )

    [1--1] => Array
    (
        [type] => Product
        [attribute] => seller_id
        [operator] => ==
        [value] => 1
    )

   [1--2] => Array
    (
        [type] => SProduct
        [attribute] => seller_id
        [operator] => ==
        [value] => 1
    )

)

我需要取消设置包含seller_id的数组。有人向我提供了代码段吗?

我试过下面的代码

<?php
foreach ($data as $key => $value){
            if(in_array('seller_id',$value)){
                array_keys($value);
            }
        }

但是使用这个我没有得到钥匙

2 个答案:

答案 0 :(得分:3)

您必须使用unset(),并使用密钥迭代数组:

foreach ($array as $key => $value) {
    if (isset($value['attribute']) && $value['attribute'] == 'seller_id') {
        unset($array[$key]);
    }
}

答案 1 :(得分:0)

尝试按照取消设置数组中的键

foreach ($array as $key => $value) {
    if (isset($value['attribute'])) {
        if($value['attribute']=='seller_id'){
            unset($array[$key];
        }
    }
}