I have a PHP array as follows:
$messages = [312, 401, 1599, 3, ...];
I want to delete the element containing the value $del_val
Or simply, manual way:
foreach ($array as $key => $value){
if ($value == $target_value) {
unset($array[$key]);
}
}
This is the safest of them because you have full control on your array
Borrowed the logic of underscore.JS _.reject and created two functions (people prefer functions!!)
array_reject_value: This function is simply rejecting the value specified (also works for PHP4,5,7)
function array_reject_value(array &$arrayToFilter, $deleteValue) {
$filteredArray = array();
foreach ($arrayToFilter as $key => $value) {
if ($value !== $deleteValue) {
$filteredArray[] = $value;
}
}
return $filteredArray;
}
array_reject: This function is simply rejecting the callable method (works for PHP >=5.3)
function array_reject(array &$arrayToFilter, callable $rejectCallback) {
$filteredArray = array();
foreach ($arrayToFilter as $key => $value) {
if (!$rejectCallback($value, $key)) {
$filteredArray[] = $value;
}
}
return $filteredArray;
}
So in our current example we can use the above functions as follows:
$messages = [312, 401, 1599, 3, 6];
$messages = array_reject_value($messages, 401);
or even better: (as this give us a better syntax to use like the array_filter one)
$messages = [312, 401, 1599, 3, 6];
$messages = array_reject($messages, function ($value) {
return $value === 401;
});
The above can be used for more complicated stuff like let's say we would like to remove all the values that are greater or equal to 401 we could simply do this:
$messages = [312, 401, 1599, 3, 6];
$greaterOrEqualThan = 401;
$messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
return $value >= $greaterOrEqualThan;
});
I know this is not efficient at all but is simple, intuitive and easy to read.
So if someone is looking for a not so fancy solution which can be extended to work with more values, or more specific conditions .. here is a simple code:
$result = array();
$del_value = 401;
//$del_values = array(... all the values you don`t wont);
foreach($arr as $key =>$value){
if ($value !== $del_value){
$result[$key] = $value;
}
//if(!in_array($value, $del_values)){
// $result[$key] = $value;
//}
//if($this->validete($value)){
// $result[$key] = $value;
//}
}
return $result
you can do:
unset($messages[array_flip($messages)['401']]);
Explanation: Delete the element that has the key 401
after flipping the array.
The accepted answer converts the array to associative array, so, if you would like to keep it as a non-associative array with the accepted answer, you may have to use array_values
too.
if(($key = array_search($del_val, $messages)) !== false) {
unset($messages[$key]);
$arr = array_values($messages);
}
The reference is here
The Best way is array_splice
array_splice($array, array_search(58, $array ), 1);
Reason for Best is here at http://www.programmerinterview.com/index.php/php-questions/how-to-delete-an-element-from-an-array-in-php/