我有这个数组:
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) ""
}
}
Run Code Online (Sandbox Code Playgroud)
我想删除子数组中"已删除"键中包含值334607943355808的所有子数组.我得到了这段代码:
if(($key = array_search("334607943355808", $array)) !== false) {
unset($array[$key]);
}
Run Code Online (Sandbox Code Playgroud)
from:PHP数组按值(不是键)删除它是非多数组的,但是我怎么能在我的情况下呢?
编辑:
我现在这样试试:
foreach($array as $delete){
if(($key = array_search("334607943355808", $delete)) !== false) {
unset($delete[$key]);
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用
只是简单地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
}
}
Run Code Online (Sandbox Code Playgroud)
或者通过主数组中的键:
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
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以deleted通过键提取值,搜索和取消设置:
if(($key = array_search('334607943355808',
array_column($array, 'deleted'))) !== false) {
unset($array[$key]);
}
Run Code Online (Sandbox Code Playgroud)