3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $a = array(0,1,2); $maxtries = intval(count($a)*(floatval(count($a)+1)/2.0)); $tries = 0; while(!empty($a) && $tries<$maxtries) { echo "Try number: ".($tries+1)."\n"; print_r($a); if($a[$tries]!=1 || $tries<3) { array_push($a, $a[$tries]); } unset($a[$tries]); $tries += 1; } echo "done\n"; print_r($a);
based on mYvsc
Output for 4.3.0 - 7.2.0
Try number: 1 Array ( [0] => 0 [1] => 1 [2] => 2 ) Try number: 2 Array ( [1] => 1 [2] => 2 [3] => 0 ) Try number: 3 Array ( [2] => 2 [3] => 0 [4] => 1 ) Try number: 4 Array ( [3] => 0 [4] => 1 [5] => 2 ) Try number: 5 Array ( [4] => 1 [5] => 2 [6] => 0 ) Try number: 6 Array ( [5] => 2 [6] => 0 ) done Array ( [6] => 0 [7] => 2 )