Interchange the display order in a loop
<p>I am trying to interchange the display order in a loop.<br /> 
For example i have an array <code>$array</code> with values: <code>1,2,3,4,5</code> and want to display the result in the order <code>2,1,3,4,5</code>. 
I am using the following code for the purpose and which worked for me:</p>

<pre><code>&lt;?php
$array = array(
    1,
    2,
    3,
    4,
    5,
);
$tempArray = array();
$count = 1;
foreach($array as $key =&gt; $value){
    $tempArray[$key] = $value;
    if(in_array($count, array(1, 2))){
        if($count == 2){
            echo $tempArray[1] . '&lt;br /&gt;';
            echo $tempArray[0] . '&lt;br /&gt;';
        }
    }else{
        echo $value . '&lt;br /&gt;';
    }
    $count++;
}
</code></pre>

<p>But i would like to know if there is any effective (better) way of doing so?</p>

<p><strong>EDIT</strong>:</p>

<pre><code>$array = array(
    1,
    2,
    3,
    4,
    5,
);
//Either
/*$temp = $array[1];
$array[1] = $array[0];
$array[0] = $temp;*/

//OR
list($array[1], $array[0]) = array($array[0], $array[1]);

foreach($array as $key =&gt; $value){
    echo $value . '&lt;br /&gt;';
}
</code></pre>

<p>Either way works fine with minimum code. <br />
Thank you guys!</p>
