PHP从关联数组中删除元素

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

I have an PHP array that looks something like this:

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

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

When I var_dump the array values i get this:

当我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" } }

I wanted to remove Completed and Mark As Spam. I know I can unset[$array[3],$array[4]), but the problem is that sometimes the index number can be different.

我想删除Completed和Mark As Spam。我知道我可以取消设置[$ array [3],$ array [4]),但问题是有时索引号可能不同。

Is there a way to remove them by matching the value name instead of the key value?

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

8 个解决方案

#1


131  

Your array is quite strange : why not just use the key 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() :

并且您可以使用函数来搜索值,例如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 ?

即使可行,也不是那么简单......我坚持认为:你能不能改变数组的格式,使用更简单的键/值系统?

#2


90  

  ...

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



  return array_values($array);
  ...

#3


13  

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

For 2D arrays...

对于2D阵列......

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

#4


2  

Try this:

尝试这个:

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

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

/ edit如JohnP所述,此方法仅适用于非嵌套数组。

#5


2  

Why do not use array_diff?

为什么不使用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.

请注意,您的阵列将被重新编制索引。

#6


2  

You can use this

你可以用它

unset($dataArray['key']);

#7


0  

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;

#8


0  

for single array Item use reset($item)

单个数组项目使用重置($ item)

#1


131  

Your array is quite strange : why not just use the key 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() :

并且您可以使用函数来搜索值,例如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 ?

即使可行,也不是那么简单......我坚持认为:你能不能改变数组的格式,使用更简单的键/值系统?

#2


90  

  ...

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



  return array_values($array);
  ...

#3


13  

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

For 2D arrays...

对于2D阵列......

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

#4


2  

Try this:

尝试这个:

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

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

/ edit如JohnP所述,此方法仅适用于非嵌套数组。

#5


2  

Why do not use array_diff?

为什么不使用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.

请注意,您的阵列将被重新编制索引。

#6


2  

You can use this

你可以用它

unset($dataArray['key']);

#7


0  

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;

#8


0  

for single array Item use reset($item)

单个数组项目使用重置($ item)