Rerun an array even if the loop will change the array elements

Php

Rerun an array even if the loop will change the array elements

I have this problem where i will need to loop an array but while looping it , i will add elements into array and i need to loop that data as well. Below are an example that you can do so:-

    $array = array('1','1.5','3','5','7');
    $count = count($array);
    for($i=0; $i<$count;$i++)
    {
        $number = $array[$i];
        echo $number."<br>";
        if($number == '3')
        {
            unset($array[$i]);
            $array[] = '2';
            $array[] = '4';
            sort($array);
            $array = array_values($array);
            $count = count($array);
            $i = -1;
        }
        elseif($number == '4')
        {
            unset($array[$i]);
            $array[] = '1';
            $array[] = '10';
            sort($array);
            $array = array_values($array);
            $count = count($array);
            $i = -1;
        }
        else
        {
            unset($array[$i]);

        }
        echo "<hr>";
    }
Back To Top