This is my basic PHP code. With only for loop and I want to sort it with this for loop only.
<?php
//this is my 2-d array
$arr=array(array(5,9),
array(7,1),
array(3,2),
array(6,4),
array(2,8));
/*my basic for loop concept*/
for($k=0;$k<count($arr);$k++)//for loop for key
{
for($v=0;$v<count($arr[$k]);$v++)//for loop for value
{
for($i=0;$i<5;$i++)//for loop to iterate
{
for($j=0;$j<3;$j++)//for loop to iterate
{
print_r($arr[$k][$v].'<br>');
if($arr[$k][$v] > $arr[$i][$j])
{
$temp = $arr[$k][$v];
$arr[$k][$v]=$arr[$i][$j];
$arr[$i][$j] = $temp;
print_r($arr[$i][$j].'<br>');
}
}
}
}
}
?>
You can use array_multisort() function of PHP
For your code the solutions will be not to use loop.
<?php
$arr=array(array(5,9),
array(7,1),
array(3,2),
array(6,4),
array(2,8));
array_multisort($arr, SORT_ASC); // For ascending
array_multisort($arr, SORT_DESC); // For Descending
print_r($arr);
?>
If you want to sort sub-array also then you can use loop and can call array_multisort() function like
array_multisort($arr[$index], SORT_ASC);
I hope this will help.
I hope this will be help for you :
<?php
//this is my 2-d array
$arr=array(array(5,9),
array(7,1),
array(3,2),
array(6,4),
array(2,8));
for($i=0;$i<count($arr);$i++){
array_multisort($arr[$i], SORT_ASC);
}
print_r($arr);
?>