Organizational Research By

Surprising Reserch Topic

Question:how to to remove a value from an array using php



asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
+1 vote
25 views



Related Hot Questions

1 Answer

0 votes

 

If you want to remove a value from an array, then there is no direct mechanism.
 
The following function uses the array_keys() function to find the key(s) of the value that you want to remove and then removes the elements for that key.
 
I've also given some examples and the output.
 
/**
  * array array_remove ( array input, mixed search_value [, bool strict] )
  **/
function array_remove(array &$a_Input, $m_SearchValue, $b_Strict = False) {
    $a_Keys = array_keys($a_Input, $m_SearchValue, $b_Strict);
    foreach($a_Keys as $s_Key) {
        unset($a_Input[$s_Key]);
    }
    return $a_Input;
}
?>
 
Beside scalar variables (integers, floats, strings, boolean), you can also use arrays as the values you want to remove.
 
// Results in array(8, 8.0, '8', '8.0')
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')), array(8));
 
// Results in array(8, 8.0, '8', '8.0', array('8'))
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')), array(8), True);
?>
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...