php remove value from array if exists code example
Example 1: searching and removing element from an array php
<?php
$hackers = array ('Alan Kay', 'Peter Norvig', 'Linus Trovalds', 'Larry Page');
print_r($hackers);
$pos = array_search('Linus Trovalds', $hackers);
echo 'Linus Trovalds found at: ' . $pos;
unset($hackers[$pos]);
print_r($hackers);
Example 2: php remove element from array
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
unset($arr['b']);
$arr = array(1, 2, 3);
array_splice($arr, 1, 1);
Example 3: php array remove value if exists
<?php
$myArray = array ('Alan', 'Peter', 'Linus', 'Larry');
$pos = array_search('Linus', $myArray);
echo 'Linus found at: '.$pos;
unset($myArray[$pos]);
print_r($myArray);
?>
Example 4: php remove element from array by value
foreach($appsList as $key => $app) {
if($app["app_status"] !== "approved") {
unset($appsList[$key]);
}
}