Difference between for and while loops with nested list ()

Can someone tell me what is the difference between these two loops / code snippets?

I get the same result, but the text book indicates that there is a difference with the outer and inner loop? Any clarification would be helpful. I don’t think I understand the list with each function.

Array definition:

$newArray  = array(array('CODE' => 'TIR', 'Description' =>'TIRES', 'Price' => 100),
                   array('CODE' => 'OIL', 'Description' => 'Oil', 'Price' =>10),
                   array('CODE' => 'SPK', 'Description' => 'Spark Plug', 'Price' =>40)
             );

Code Snippet 1:

for ($row = 0; $row < 3; $row ++)
{           
    echo ' |'.$newArray[$row]['CODE'].'| '.$newArray[$row]['Description']. '| '.$newArray[$row]['Price'];
    echo  '<br />';
    echo  '<br />';      
}

Code Snippet 2:

for ($row =0; $row <3; $row ++)
{
    while (list($key, $value) = each ($newArray[$row]))
    {
        echo "|$value";
    }
    echo  '<br />';
    echo  '<br />';
}
+4
source share
1 answer

Yes, there is a difference if you look at it from the point of view of the algorithm.

The first has a time complexity of O (n), and the second is O (n 2 ).

So, the first one is more effective.

+1
source

All Articles