删除关联数组的一部分[重复]

时间:2022-08-26 14:15:29

This question already has an answer here:

这个问题在这里已有答案:

I want to search an associative array and when I find a value, delete that part of the array.

我想搜索一个关联数组,当我找到一个值时,删除该数组的那一部分。

Here is a sample of my array:

这是我的数组的示例:

    Array
(
    [0] => Array
        (
            [id] => 2918
            [schoolname] => Albany Medical College
            [AppService] => 16295C0C51D8318C2
        )

    [1] => Array
        (
            [id] => 2919
            [schoolname] => Albert Einstein College of Medicine
            [AppService] => 16295C0C51D8318C2
        )

    [2] => Array
        (
            [id] => 2920
            [schoolname] => Baylor College of Medicine
            [AppService] => 16295C0C51D8318C2
        )
}

What I want to do is find the value 16295C0C51D8318C2 in the AppService and then delete that part of the array. So, for example, if that code was to run on the above array, it was empty out the entire array since the logic matches everything in that array.

我想要做的是在AppService中找到值16295C0C51D8318C2,然后删除该部分数组。因此,例如,如果该代码是在上面的数组上运行的,那么整个数组都是空的,因为逻辑匹配该数组中的所有内容。

Here is my code so far:

这是我到目前为止的代码:

            foreach($this->schs_raw as $object) {
                if($object['AppService'] == "16295C0C51D8318C2") {
                    unset($object);
                }
        }

4 个解决方案

#1


2  

Try like this:

试试这样:

foreach ($this->schs_raw as &$object) {
    if($object['AppService'] == "16295C0C51D8318C2") {
        unset($object);
    }
}

Eventually:

最后:

foreach ($this->schs_raw as $k => $object) {
    if($object['AppService'] == "16295C0C51D8318C2") {
        unset($this->schs_raw[$k]);
    }
}

#2


4  

array_filter will help (http://php.net/manual/en/function.array-filter.php)

array_filter会有所帮助(http://php.net/manual/en/function.array-filter.php)

$yourFilteredArray = array_filter(
    $this->schs_raw,
    function($var) {
        return $object['AppService'] != "16295C0C51D8318C2"
    }
);

#3


2  

Try this:

尝试这个:

foreach($this->schs_raw as $key=>$object) {
  if($object['AppService'] == "16295C0C51D8318C2") {      
    unset($this->schs_raw[$key]); // unset the array using appropriate index    
    break; // to exit loop after removing first item
  }
}

#4


0  

Why not create a new array? If match not found, add index to new array... If it is found, don't add it. Your new array will only contain the data you want.

为什么不创建一个新阵列?如果未找到匹配项,请将索引添加到新阵列...如果找到,请不要添加它。您的新阵列将只包含您想要的数据。

#1


2  

Try like this:

试试这样:

foreach ($this->schs_raw as &$object) {
    if($object['AppService'] == "16295C0C51D8318C2") {
        unset($object);
    }
}

Eventually:

最后:

foreach ($this->schs_raw as $k => $object) {
    if($object['AppService'] == "16295C0C51D8318C2") {
        unset($this->schs_raw[$k]);
    }
}

#2


4  

array_filter will help (http://php.net/manual/en/function.array-filter.php)

array_filter会有所帮助(http://php.net/manual/en/function.array-filter.php)

$yourFilteredArray = array_filter(
    $this->schs_raw,
    function($var) {
        return $object['AppService'] != "16295C0C51D8318C2"
    }
);

#3


2  

Try this:

尝试这个:

foreach($this->schs_raw as $key=>$object) {
  if($object['AppService'] == "16295C0C51D8318C2") {      
    unset($this->schs_raw[$key]); // unset the array using appropriate index    
    break; // to exit loop after removing first item
  }
}

#4


0  

Why not create a new array? If match not found, add index to new array... If it is found, don't add it. Your new array will only contain the data you want.

为什么不创建一个新阵列?如果未找到匹配项,请将索引添加到新阵列...如果找到,请不要添加它。您的新阵列将只包含您想要的数据。