1

I want to sort the below array with the value of 'fee_category'. The first key of the array is date and inside each date it may contain multiple fees as in the 4th and 5th dates (which is [2020-10-15] and [2020-10-14]). I just need a simple way to get this sorting done. I tried usort(), but didn't work as expected.

Array(
    [2020-10-19] => 
    Array(
        [0] => Array(
            [fee_fare] => 2650
            [fee_category] => Tuition Fee
        )
    )
    [2020-10-17] => 
    Array(
        [0] => Array(
            [fee_fare] => 630
            [fee_category] => Special Fee
        )
    )
    [2020-10-16] => 
    Array(
        [0] => Array(
            [fee_fare] => 800
            [fee_category] => Special Fee
        )
    )
    [2020-10-15] => 
    Array(
        [0] => Array(
            [fee_fare] => 550
            [fee_category] => Special Fee
        )
        [1] => Array(
            [fee_fare] => 550
            [fee_category] => Special Fee
        )
        [2] => Array(
            [fee_fare] => 2650
            [fee_category] => Tuition Fee
        )
        [3] => Array(
            [fee_fare] => 3850
            [fee_category] => Tuition Fee
        )
        [4] => Array(
            [fee_fare] => 630
            [fee_category] => Special Fee
        )
    )
    [2020-10-14] => 
    Array(
        [0] => Array(
            [fee_fare] => 550
            [fee_category] => Special Fee
        )
        [1] => Array(
            [fee_fare] => 2650
            [fee_category] => Tuition Fee
        )
        [2] => Array(
            [fee_fare] => 550
            [fee_category] => Special Fee
        )
        [3] => Array(
            [fee_fare] => 1325
            [fee_category] => Tuition Fee
        )
        [4] => Array(
            [fee_fare] => 550
            [fee_category] => Special Fee
        )
        [5] => Array([fee_fare] => 800
            [fee_category] => Special Fee
        )
        [6] => Array([fee_fare] => 2700
            [fee_category] => Tuition Fee
        )
    )
)

expected output

Array(
[2020-10-19] => 
Array(
    [0] => Array(
        [fee_fare] => 2650
        [fee_category] => Tuition Fee
    )
)
[2020-10-17] => 
Array(
    [0] => Array(
        [fee_fare] => 630
        [fee_category] => Special Fee
    )
)
[2020-10-16] => 
Array(
    [0] => Array(
        [fee_fare] => 800
        [fee_category] => Special Fee
    )
)
[2020-10-15] => 
Array(
    [0] => Array(
        [fee_fare] => 550
        [fee_category] => Special Fee
    )
    [1] => Array(
        [fee_fare] => 550
        [fee_category] => Special Fee
    )
    [4] => Array(
        [fee_fare] => 630
        [fee_category] => Special Fee
    )
    [2] => Array(
        [fee_fare] => 2650
        [fee_category] => Tuition Fee
    )
    [3] => Array(
        [fee_fare] => 3850
        [fee_category] => Tuition Fee
    )
)
[2020-10-14] => 
Array(
    [0] => Array(
        [fee_fare] => 550
        [fee_category] => Special Fee
    )
    [2] => Array(
        [fee_fare] => 550
        [fee_category] => Special Fee
    )
    [4] => Array(
        [fee_fare] => 550
        [fee_category] => Special Fee
    )
    [5] => Array(
        [fee_fare] => 800
        [fee_category] => Special Fee
    )
    [1] => Array(
        [fee_fare] => 2650
        [fee_category] => Tuition Fee
    )
    [3] => Array(
        [fee_fare] => 1325
        [fee_category] => Tuition Fee
    )
    [6] => Array([fee_fare] => 2700
        [fee_category] => Tuition Fee
    )
)

)

Thanks in advance.

1
  • 1
    Please share your attempt and explain in what way it didn't give you the results you desired. Commented Oct 20, 2020 at 6:44

1 Answer 1

1

Here is what you need array_multisort combined with array_column

$arr = [
    '2020-10-19' =>
        [
            0 => [
                'fee_fare' => 2650,
                'fee_category' => 'Tuition Fee'
            ]
        ],
    '2020-10-17' =>
        [
            0 => [
                'fee_fare' => 630,
                'fee_category' => 'Special Fee'
            ]
        ],
    '2020-10-16' =>
        [
            0 => [
                'fee_fare' => 800,
                'fee_category' => 'Special Fee'
            ]
        ],
    '2020-10-15' =>
        [
            0 => [
                'fee_fare' => 550,
                'fee_category' => 'Special Fee'
            ],
            1 => [
                'fee_fare' => 550,
                'fee_category' => 'Special Fee'
            ],
            2 => [
                'fee_fare' => 2650,
                'fee_category' => 'Tuition Fee'
            ],
            3 => [
                'fee_fare' => 3850,
                'fee_category' => 'Tuition Fee'
            ],
            4 => [
                'fee_fare' => 630,
                'fee_category' => 'Special Fee'
            ],
        ],
    '2020-10-14' => [
        0 => [
            'fee_fare' => 550,
            'fee_category' => 'Special Fee'
        ],
        1 => [
            'fee_fare' => 2650,
            'fee_category' => 'Tuition Fee'
        ],
        2 => [
            'fee_fare' => 550,
            'fee_category' => 'Special Fee'
        ],
        3 => [
            'fee_fare' => 1325,
            'fee_category' => 'Tuition Fee'
        ],
        4 => [
            'fee_fare' => 550,
            'fee_category' => 'Special Fee'
        ],
        5 => [
            'fee_fare' => 800,
            'fee_category' => 'Special Fee'
        ],
        6 => [
            'fee_fare' => 2700,
            'fee_category' => 'Tuition Fee'
        ]
    ]
];

foreach($arr as $key => $a){
    array_multisort(
        array_column($a, 'fee_fare'),
        SORT_ASC,
        $a
    );
    $arr[$key] = $a;
}



echo "<pre>";
echo "<b>".__FILE__."</b><br/>";
var_dump($arr);
echo "</pre>";
die();

Otput is :

array (size=5)
  '2020-10-19' => 
    array (size=1)
      0 => 
        array (size=2)
          'fee_fare' => int 2650
          'fee_category' => string 'Tuition Fee' (length=11)
  '2020-10-17' => 
    array (size=1)
      0 => 
        array (size=2)
          'fee_fare' => int 630
          'fee_category' => string 'Special Fee' (length=11)
  '2020-10-16' => 
    array (size=1)
      0 => 
        array (size=2)
          'fee_fare' => int 800
          'fee_category' => string 'Special Fee' (length=11)
  '2020-10-15' => 
    array (size=5)
      0 => 
        array (size=2)
          'fee_fare' => int 550
          'fee_category' => string 'Special Fee' (length=11)
      1 => 
        array (size=2)
          'fee_fare' => int 550
          'fee_category' => string 'Special Fee' (length=11)
      2 => 
        array (size=2)
          'fee_fare' => int 630
          'fee_category' => string 'Special Fee' (length=11)
      3 => 
        array (size=2)
          'fee_fare' => int 2650
          'fee_category' => string 'Tuition Fee' (length=11)
      4 => 
        array (size=2)
          'fee_fare' => int 3850
          'fee_category' => string 'Tuition Fee' (length=11)
  '2020-10-14' => 
    array (size=7)
      0 => 
        array (size=2)
          'fee_fare' => int 550
          'fee_category' => string 'Special Fee' (length=11)
      1 => 
        array (size=2)
          'fee_fare' => int 550
          'fee_category' => string 'Special Fee' (length=11)
      2 => 
        array (size=2)
          'fee_fare' => int 550
          'fee_category' => string 'Special Fee' (length=11)
      3 => 
        array (size=2)
          'fee_fare' => int 800
          'fee_category' => string 'Special Fee' (length=11)
      4 => 
        array (size=2)
          'fee_fare' => int 1325
          'fee_category' => string 'Tuition Fee' (length=11)
      5 => 
        array (size=2)
          'fee_fare' => int 2650
          'fee_category' => string 'Tuition Fee' (length=11)
      6 => 
        array (size=2)
          'fee_fare' => int 2700
          'fee_category' => string 'Tuition Fee' (length=11)

As you can see we loop through the "main" array, splitting it to $key => $value, then we use array_multisort on the sub-array by the value of 'fee_fare' column

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.