Trying to remove all elements in an array (below) that are less than 0 using the following code:
<?php
$arr=array(1,2,3,4,5,-6,-7,-8,-9,-10);
for ($i=0;$i<count($arr);$i++){
if ($arr[$i]<0) {
unset($arr[$i]);
}
}
var_dump($arr);
echo '<pre>', print_r($arr), '</pre>';
?>
strong text
However, the result is as follows:
array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [8]=> int(-9) [9]=> int(-10) }
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[8] => -9
[9] => -10
)
1
It is rather vague why not all elements that are less than 0 are removed from the array. Any thoughts on this? thanks in advance.
source
share