Home > database >  Multisort subarrays -- one which contains ranged data
Multisort subarrays -- one which contains ranged data

Time:12-08

I have an multidimensional array and need to sort it to display proper data. below is the example

$specification = array(
    'type' => array(
        [0] => 'Excavator',
        [1] => 'Excavator',
        [2] => 'Excavator',
        [3] => 'Excavator',
        [4] => 'Mini Excavator',
        [5] => 'Mini Excavator',
        [6] => 'Rubber Tire',
        [7] => 'Rubber Tire',
        [8] => 'Rubber Tire',
    ),
    'estimated_weight' => array(
        [0] => '750',
        [1] => '750',
        [2] => '675',
        [3] => '680',
        [4] => '110',
        [5] => '130',
        [6] => '200',
        [7] => '300',
        [8] => '200',
    ),
    'weight_class' => array(
        [0] => '24,000 - 33,000 Lbs',
        [1] => '24,000 - 33,000 Lbs',
        [2] => '40,000 - 45,000 Lbs',
        [3] => '40,000 - 45,000 Lbs',
        [4] => '2,500 - 4,000 Lbs',
        [5] => '4,000 - 8,000 Lbs',
        [6] => 'Rubber Tire',
        [7] => 'Rubber Tire',
        [8] => 'Rubber Tire',
    ),
);

and I want a array with sorted data by estimated weight & Weight Class simultaneously, I have tried multi sort but not able to find proper solution I have also tried usort but it seems not working, need help to sort this out.

array_multisort(
    array_column($specification, 'weight_class'), SORT_ASC,
    array_column($specification, 'estimated_weight'), SORT_ASC,
    $specification
);

Expected result is sorted with estimated_weight along with weight_class as Rubber tire is text and other data are numeric Rubber tire will be at last.

$specification = array(
    'type' => array(
        [0] => 'Mini Excavator',
        [1] => 'Mini Excavator',
        [2] => 'Excavator',
        [3] => 'Excavator',
        [4] => 'Excavator',
        [5] => 'Excavator',
        [6] => 'Rubber Tire',
        [7] => 'Rubber Tire',
        [8] => 'Rubber Tire',
    ),
    'estimated_weight' => array(
        [0] => '110',
        [1] => '130',
        [2] => '675',
        [3] => '680',
        [4] => '750',
        [5] => '750',
        [6] => '200',
        [7] => '200',
        [8] => '300',
    ),
    'weight_class' => array(
        [0] => '2,500 - 4,000 Lbs',
        [1] => '4,000 - 8,000 Lbs',
        [2] => '24,000 - 33,000 Lbs',
        [3] => '24,000 - 33,000 Lbs',
        [4] => '40,000 - 45,000 Lbs',
        [5] => '40,000 - 45,000 Lbs',
        [6] => 'Rubber Tire',
        [7] => 'Rubber Tire',
        [8] => 'Rubber Tire',
    ),
);

CodePudding user response:

You don't need array_column() to access the first-level associative elements.

Use the SORT_NATURAL flag to sort the hyphenated values correctly.

You mustn't nominate the whole array as the final parameter because using an array with a size (3) unequal to all other array sizes (9) will break the function.

Logically, you do not want to reorder the associative element positions; you merely want to synchronously reorder the subarrays.

It is not necessary to declare SORT_ASC as a parameter -- it is the default sorting order.

Code: (Demo)

array_multisort(
    $specification['weight_class'],
    SORT_NATURAL,
    $specification['estimated_weight'],
    $specification['type'],
);
var_export($specification);
  • Related