PHP通过子数组键值从多维数组中删除子数组

时间:2022-09-25 08:58:02

I've got this array:

我有这个数组:

array(5) {
  [0]=>
  array(4) {
    ["nachricht"]=>
    string(9) "blablaaaa"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:13"
    ["deleted"]=>
    string(0) ""
  }
  [1]=>
  array(4) {
    ["nachricht"]=>
    string(3) "joo"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(15) "334607943355808"
  }
  [2]=>
  array(4) {
    ["nachricht"]=>
    string(4) "noma"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
  [3]=>
  array(4) {
    ["nachricht"]=>
    string(4) "test"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
  [4]=>
  array(4) {
    ["nachricht"]=>
    string(4) "doh!"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
}

I want to delete all sub arrays which include the value 334607943355808 in the key "deleted" in the sub array. I got this code:

我想删除所有子数组,其中包括子数组“已删除”键中的值334607943355808。我有这段代码:

if(($key = array_search("334607943355808", $array)) !== false) {
            unset($array[$key]);
        }

from: PHP array delete by value (not key) where it's non multi-array, but how can I do it in my case?

从:PHP数组按值(不是键)删除,因为它不是多数组,但是在我的例子中我怎么做呢?

EDIT:

编辑:

I tryed it this way now:

我现在这样想:

foreach($array as $delete){
      if(($key = array_search("334607943355808", $delete)) !== false) {
                unset($delete[$key]);
      }
}

But it's not working

但这不是工作

2 个解决方案

#1


5  

Just a simple foreach with a reference to the sub array:

只是一个简单的foreach和对子数组的引用:

foreach($array as &$sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        $sub_array = null;
        break; //if there will be only one then break out of loop
    }
}

Or by key in the main array:

或在主数组中按键:

foreach($array as $key => $sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        unset($array[$key]);
        break; //if there will be only one then break out of loop
    }
}

You could also extract the deleted values, search and unset by key:

您还可以提取已删除的值、搜索和按键取消设置:

if(($key = array_search('334607943355808',
                        array_column($array, 'deleted'))) !== false) {
    unset($array[$key]);
}

#2


0  

You can use array_map().Try this

您可以使用到()。试试这个

$finalArr = array_map(function($v){
    if($v['deleted'] == '334607943355808') unset($v['deleted']);
    return $v;
}, $arr);

#1


5  

Just a simple foreach with a reference to the sub array:

只是一个简单的foreach和对子数组的引用:

foreach($array as &$sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        $sub_array = null;
        break; //if there will be only one then break out of loop
    }
}

Or by key in the main array:

或在主数组中按键:

foreach($array as $key => $sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        unset($array[$key]);
        break; //if there will be only one then break out of loop
    }
}

You could also extract the deleted values, search and unset by key:

您还可以提取已删除的值、搜索和按键取消设置:

if(($key = array_search('334607943355808',
                        array_column($array, 'deleted'))) !== false) {
    unset($array[$key]);
}

#2


0  

You can use array_map().Try this

您可以使用到()。试试这个

$finalArr = array_map(function($v){
    if($v['deleted'] == '334607943355808') unset($v['deleted']);
    return $v;
}, $arr);