0

I have below array

Array
(
    [0] => Array
        (
            [ft_name] => workout
            [days] => 2
        )

    [1] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [2] => Array
        (
            [ft_name] => df
            [days] => 3
        )

    [3] => Array
        (
            [ft_name] => df
            [days] => 1
        )

    [4] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [5] => Array
        (
            [ft_name] => ss
            [days] => 6
        )

    [6] => Array
        (
            [ft_name] => reday
            [days] => 5
        )

)

I want it in this format like the position of array having ft_name=>"restday" should be remain same and the rest are should be sorted in order by days field

I have tried with this code but result is not expected.

<?php    
$size = count($val);
    for($i=0;$i< $size;$i++){
        if($val[$i]['days']){
            for ($j=0; $j<$size-$i; $j++) {

                if(!$val[$j+1]['days']) continue;   

                if ($val[$j+1]['days'] < $val[$j]['days']) {
                      swap($val, $j, $j+1);
                }
            }
        }
        else{
            continue;
        }

    }
    echo "<pre>"; print_r($val);

    function swap(&$arr, $a, $b) {
        $tmp = $arr[$a];
        $arr[$a] = $arr[$b];
        $arr[$b] = $tmp;
    }
?>

Output should be looks like this one

Array
(
    [0] => Array
        (
            [ft_name] => df
            [days] => 1
        )

    [1] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [2] => Array
        (
            [ft_name] => workout
            [days] => 2
        )

    [3] => Array
        (
            [ft_name] => df
            [days] => 4
        )        

    [4] => Array
        (
            [ft_name] => restday
            [days] => 
        )

    [5] => Array
        (
            [ft_name] => reday
            [days] => 5
        )

    [6] => Array
        (
            [ft_name] => ss
            [days] => 6
        )

)

Thanks in advance.

Praveen kalal
  • 2,148
  • 4
  • 19
  • 33

2 Answers2

3

You should try array_multisort , here you have the documentation

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    if ( $row['days'] != '' ){
        $days[$key]  = $row['days'];
        $name[$key] = $row['ft_name']; 
    }else{
        $days[$key] = $key; 
        $name[$key] = $row['ft_name']; 
        }
    }

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($days, SORT_ASC, $name, SORT_ASC, $data);
?>

Here you have a nice post about PHP Sorting arrays

Community
  • 1
  • 1
David
  • 1,147
  • 4
  • 17
  • 29
  • Thanks, I have already tried this code but its changing the position of restday too and I want to keep the position of restday as same. – Praveen kalal May 20 '16 at 06:38
  • you can try adding an if condition inside the foreach, i will update the answer – David May 20 '16 at 06:50
1

Use usort() with a callback function having 2 tests one the main field and if the main field has equal value, test the second field

You can downvote me I misunderstood

Darren
  • 13,050
  • 4
  • 41
  • 79
quazardous
  • 846
  • 10
  • 15