// Ascending order function
function bubbleSortAsc(array $arr)
{
$sorted = FALSE;
while ($sorted === FALSE)
{
$sorted = TRUE;
for ($i = 0; $i < count($arr)-1; ++$i)
{
$current = $arr[$i];
$next = $arr[$i+1];
if ($next < $current)
{
$arr[$i] = $next;
$arr[$i+1] = $current;
$sorted = FALSE;
}
}
}
return $arr;
}
// Descending order function
function bubbleSortDesc(array $arr)
{
$sorted = FALSE;
while ($sorted === FALSE)
{
$sorted = TRUE;
for ($i = 0; $i < count($arr)-1; ++$i)
{
$current = $arr[$i];
$next = $arr[$i+1];
if ($next > $current)
{
$arr[$i] = $next;
$arr[$i+1] = $current;
$sorted = FALSE;
}
}
}
return $arr;
}
// Call any function
$arr_val = array(50, 1, 45, 10, 40, 20, 35, 30, 3, 2);
$sortedArr = bubbleSortAsc($arr_val);
print_r($sortedArr);
?>