PHP Remove items from an associative array

I have a PHP array that looks something like this:

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 values โ€‹โ€‹of an array, I get the following:

 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 that I can unset[$array[3],$array[4]) , but the problem is that sometimes the index number may be different.

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

+91
arrays php
Mar 27 2018-11-11T00:
source share
9 answers

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

It would not be much easier if your array were declared like this:

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

This will allow you to use your key values โ€‹โ€‹as indexes to access the array ...

And you can use functions to search by 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 will need to iterate over all the elements, analyze the value and reset the necessary elements:

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

Even if it is doable, it is not so simple ... and I insist: can you change the format of your array to work with a simpler key / value system?

+140
Mar 27 '11 at 3:19
source share
  ... $array = array( 1 => 'Awaiting for Confirmation', 2 => 'Asssigned', 3 => 'In Progress', 4 => 'Completed', 5 => 'Mark As Spam', ); return array_values($array); ... 
+94
Jun 23 '12 at 1:10
source share
 $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]); } } 
+13
Mar 27 '11 at 3:19
source share

You can use this

 unset($dataArray['key']); 
+4
Sep 12 '17 at 7:01 on
source share

Why 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); 

Note that your array will be reindexed.

+3
May 13 '16 at 17:43
source share

Try the following:

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

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

+1
Mar 27 2018-11-11T00:
source share

The way to do this is to take your nested target array and copy it in one step onto 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 the code to make it simple:

 $temp_array = $list['resultset'][0]; unset($temp_array['badkey1']); unset($temp_array['badkey2']); $list['resultset'][0] = $temp_array; 
0
Sep 20 '13 at 13:28
source share

I kind of disagree with the accepted answer. Sometimes the application architecture does not want you to contact the array identifier, or makes it inconvenient. For example, I use CakePHP quite often, and a database query returns a primary key as a value in each record, very similar to the one described above.

Assuming the array is not too big, I would use array_filter. This will create a copy of the array, with the exception of the entries you want to delete, which you can assign to the original variable of the array.

Although this may seem inefficient, it is very popular these days for variables to be immutable, and the fact that most php array functions return a new array rather than merge with the original implies that PHP also wants you to do this too. And the more you work with arrays and understand how complicated and annoying the unset () function is, this approach makes a lot of sense.

However:

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

You can use any inclusion logic (for example, your identifier field) in the built-in function that you want.

0
Apr 30 '19 at 17:59
source share

for a single array Using the reset($item) element reset($item)

-one
Mar 16 '14 at 9:57
source share



All Articles