<?php $a = [5,4,3,2,1,1,2,3,4,5]; $length = count($a); for ($j = 0; $j < $length-1; $j++){ for ($i = 0; $i < $length - $j - 1; $i++){ $next = $i + 1; if ($a[$i] > $a[$next]){ $tmp = $a[$i]; $a[$i] = $a[$next]; $a[$next] = $tmp; } } } var_export($a);