Richard Quadling
asked on
Remove element from an array.
Cannot see the wood for the trees, so this is going to be some EASY points!
I have a dynamic array; i.e. at RUNTIME, values are added and removed.
Well, added is OK.
$a_Array[] = 'value';
By using in_array() I know if the value is already in the array.
How do I remove the value from the array? I have a stupidly convoluted way...
Get the key of the value and then unset that element.
<?php
$a = array('one','two',4 => 'three', 3 => 'four'); // NOT a typo!!!!
unset($a[reset(array_keys( $a, 'three', True))]);
var_export($a);
?>
This is OK, the output is ...
array (
0 => 'one',
1 => 'two',
3 => 'four',
)
but ...
Strict Standards: Only variables should be passed by reference on line 3
So, I suspect there is really a array_remove(&$array, $value) function SOMEWHERE.
I have a dynamic array; i.e. at RUNTIME, values are added and removed.
Well, added is OK.
$a_Array[] = 'value';
By using in_array() I know if the value is already in the array.
How do I remove the value from the array? I have a stupidly convoluted way...
Get the key of the value and then unset that element.
<?php
$a = array('one','two',4 => 'three', 3 => 'four'); // NOT a typo!!!!
unset($a[reset(array_keys(
var_export($a);
?>
This is OK, the output is ...
array (
0 => 'one',
1 => 'two',
3 => 'four',
)
but ...
Strict Standards: Only variables should be passed by reference on line 3
So, I suspect there is really a array_remove(&$array, $value) function SOMEWHERE.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. That was WAY too easy!
/**
* 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;
}