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

问题描述 投票:-2回答:2

我有这样的二维数组

 $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);
            }
        }

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

php arrays
2个回答
3
投票

你必须使用unset(),并使用键遍历数组:

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

0
投票

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

foreach ($array as $key => $value) {
    if (isset($value['attribute'])) {
        if($value['attribute']=='seller_id'){
            unset($array[$key];
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.