PHP: Remove all fcn not acting as expected, Code Inside

Posted by Josh K on Stack Overflow See other posts from Stack Overflow or by Josh K
Published on 2010-05-19T16:01:35Z Indexed on 2010/05/19 16:20 UTC
Read the original article Hit count: 129

Filed under:
|

I made this simple function (remove all $elem from $array):

function remall($array, $elem) {
    for($i=0; $i < count($array); $i++)
        if($array[$i] == $elem)
            unset($array[$i]);
    $newarray = array_values($array);
    return $newarray;
}

But it isn't working perfectly, here are some inputs and outputs

$u = array(1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7);
$r = remall($u, 7);
Output of $r: 12345767

$n = array(7, 7, 1, 7, 3, 4, 6, 7, 2, 3, 1, -3, 10, 11, 7, 7, 7, 2, 7);
$r = remall($n, 7);
Output of $r: 1346231-30117727

Notice how there are still 7s in my outputs. Also, My function will only be removing numbers from an array. Let me know if you spot something, thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about removeall