从关联数组中删除元素

我有一个 PHP 数组,看起来像这样:

Index              Key     Value
[0]                1       Awaiting for Confirmation
[1]                2       Assigned
[2]                3       In Progress
[3]                4       Completed
[4]                5       Mark As Spam

当我转储 var _ dump 数组值时,我得到如下结果:

array(5) { [0]=> array(2) { ["key"]=> string(1) "1" ["value"]=> string(25) "Awaiting for Confirmation" } [1]=> array(2) { ["key"]=> string(1) "2" ["value"]=> string(9) "Assigned" } [2]=> array(2) { ["key"]=> string(1) "3" ["value"]=> string(11) "In Progress" } [3]=> array(2) { ["key"]=> string(1) "4" ["value"]=> string(9) "Completed" } [4]=> array(2) { ["key"]=> string(1) "5" ["value"]=> string(12) "Mark As Spam" } }

我想移除 完成了标记为垃圾邮件。我知道我可以 unset[$array[3],$array[4]),但问题是,有时索引号可以不同。

有没有办法通过匹配值名而不是键值来删除它们?

417256 次浏览
$key = array_search("Mark As Spam", $array);
unset($array[$key]);

For 2D arrays...

$remove = array("Mark As Spam", "Completed");
foreach($arrays as $array){
foreach($array as $key => $value){
if(in_array($value, $remove)) unset($array[$key]);
}
}

Your array is quite strange : why not just use the ABC0 as index, and the value as... the value ?

Wouldn't it be a lot easier if your array was declared like this :

$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);

That would allow you to use your values of key as indexes to access the array...

And you'd be able to use functions to search on the values, such as array_search() :

$indexCompleted = array_search('Completed', $array);
unset($array[$indexCompleted]);


$indexSpam = array_search('Mark As Spam', $array);
unset($array[$indexSpam]);


var_dump($array);

Easier than with your array, no ?



Instead, with your array that looks like this :

$array = array(
array('key' => 1, 'value' => 'Awaiting for Confirmation'),
array('key' => 2, 'value' => 'Asssigned'),
array('key' => 3, 'value' => 'In Progress'),
array('key' => 4, 'value' => 'Completed'),
array('key' => 5, 'value' => 'Mark As Spam'),
);

You'll have to loop over all items, to analyse the value, and unset the right items :

foreach ($array as $index => $data) {
if ($data['value'] == 'Completed' || $data['value'] == 'Mark As Spam') {
unset($array[$index]);
}
}
var_dump($array);

Even if do-able, it's not that simple... and I insist : can you not change the format of your array, to work with a simpler key/value system ?

Try this:

$keys = array_keys($array, "Completed");

/edit As mentioned by JohnP, this method only works for non-nested arrays.

  ...


$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);






return array_values($array);
...

The way to do this to take your nested target array and copy it in single step to a non-nested array. Delete the key(s) and then assign the final trimmed array to the nested node of the earlier array. Here is a code to make it simple:

$temp_array = $list['resultset'][0];


unset($temp_array['badkey1']);
unset($temp_array['badkey2']);


$list['resultset'][0] = $temp_array;

for single array Item use reset($item)

Why do not use array_diff?

$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
$to_delete = array('Completed', 'Mark As Spam');
$array = array_diff($array, $to_delete);

Just note that your array would be reindexed.

You can use this

unset($dataArray['key']);

I kinda disagree with the accepted answer. Sometimes an application architecture doesn't want you to mess with the array id, or makes it inconvenient. For instance, I use CakePHP quite a lot, and a database query returns the primary key as a value in each record, very similar to the above.

Assuming the array is not stupidly large, I would use array_filter. This will create a copy of the array, minus the records you want to remove, which you can assign back to the original array variable.

Although this may seem inefficient it's actually very much in vogue these days to have variables be immutable, and the fact that most php array functions return a new array rather than futzing with the original implies that PHP kinda wants you to do this too. And the more you work with arrays, and realize how difficult and annoying the unset() function is, this approach makes a lot of sense.

Anyway:

$my_array = array_filter($my_array,
function($el) {
return $el["value"]!="Completed" && $el!["value"]!="Marked as Spam";
});

You can use whatever inclusion logic (eg. your id field) in the embedded function that you want.