PHP Remove elements from associative array

I have an 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 array values i get this:

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.

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

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() :

$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 ?

How can I remove a key and its value from an associative array , 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� remove/delete elements/values, array from array in PHP. Here you have learned how to remove array inside array PHP, remove an element from array PHP, remove the key from associative array PHP, remove array from multidimensional array PHP, how to remove the particular key from an array in PHP, remove array inside array PHP. You may like

  ...

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



  return array_values($array);
  ...

PHP Remove elements from associative array, Answer: Use the PHP unset() Function If you want to delete an element from an array you can simply use the unset() function. The following example shows how to delete an element from an associative array and numeric array. On this page we demonstrate and describe the various ways that you can remove elements from arrays in PHP. We cover the array_pop, array_shift, and unset functions. The array_splice function, which can also be used to remove array elements, is discussed elsewhere.

$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]);
    }
}

How to Delete an Element from an Array in PHP, The array_splice function, which can also be used to remove array elements, We can use the unset function to remove a key/value pair from an associative� PHP program to delete an element from the array using the unset function PHP Server Side Programming Programming To delete an element from the array using the unset function, the PHP code is as follows −

You can use this

unset($dataArray['key']);

How to Remove Elements from Arrays in PHP, 5, PHP 7). array_shift — Shift an element off the beginning of array I needed to remove the first set of keys and values from an associative array. Had to write� Php Delete an element from array – You can remove array element using the unset ($position) function. It deletes the element at the specified position. Here in this tutorial we are going to explain how you can delete an element form an array. We will explain this with example and demo.

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.

array_shift - Manual, array_splice — Remove a portion of the array and replace it with something else Removes the elements designated by offset and length from the input array, and When trying to splice an associative array into another, array_splice is� Use this function to remove specific arrays of keys without modifying the original array: function array_except($array, $keys) { return array_diff_key($array, array_flip((array) $keys)); } First param pass all array, second param set array of keys to remove.

array_splice - Manual, array_values() returns all the values from the array and indexes the array numerically. This version will only re-index numeric keys, leaving associative array array, but as per array_values() adjusting the keys rater than removing them. Well, deleting an element from array is basically just set difference with one element. array_diff( [312, 401, 15, 401, 3], [401] ) // removing 401 returns [312, 15, 3] It generalizes nicely, you can remove as many elements as you like at the same time, if you want.

array_values - Manual, To remove an array element by its key, provide the unset() function with will also change the keys of associative array to numerical indices. Because array_filter() preserves keys, you should consider the resulting array to be an associative array even if the original array had integer keys for there may be holes in your sequence of keys. This means that, for example, json_encode() will convert your result array into an object instead of an array.

How can I Delete an Element from an Array in PHP?, Example. Remove the first element (red) from an array, and return the value of the removed element: <?php $a=array("a"=>"red","b"=>"green","c"=>"blue"); Using unset () Function: The unset () function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change.

Comments
  • not sure whether I understand the question at hand here? What do you mean sometimes the index number can be different?
  • "You'll have to loop over all items, to analyse the value". When you're using array_search you might also search through all items.
  • Thanks, i was looking for this only.! :)
  • nice and easy . just looking for this
  • This is the true answer, not that was checked.
  • The reset function should be used to move the internal array pointer, not to remove items or unset items from an array. Use unset instead. If your solution even works, it also has unwanted side effects.