PHP Delete element from array with array_splice()

array_splice()

If you use array_splice() the keys will be automatically reindexed, but the associative keys won’t change opposed to array_values() which will convert all keys to numerical keys.

Also array_splice() needs the offset, not the key!, as second parameter.

Code

php

    $array = array(0 => "a", 1 => "b", 2 => "c");
    array_splice($array, 1, 1);
                       //↑ Offset which you want to delete

?>

Output

Array (
    [0] => a
    [1] => c
)

array_splice() same as unset() take the array by reference, this means you don’t want to assign the return values of those functions back to the array.

PHP Delete element from array with array_splice()

PHP check mobile browser

function isMobileDevice(){
$aMobileUA = array(
‘/iphone/i’ => ‘iPhone’,
‘/ipod/i’ => ‘iPod’,
‘/ipad/i’ => ‘iPad’,
‘/android/i’ => ‘Android’,
‘/blackberry/i’ => ‘BlackBerry’,
‘/webos/i’ => ‘Mobile’
);

//Return true if Mobile User Agent is detected
foreach($aMobileUA as $sMobileKey => $sMobileOS){
if(preg_match($sMobileKey, $_SERVER[‘HTTP_USER_AGENT’])){
return true;
}
}
//Otherwise return false..
return false;
}

PHP check mobile browser