Search code examples
phparrayssortingmultidimensional-array

Sort the parent level of a 3d array by a column in the deepest subarray descending


I need to sort the following array in descending order by ref_no. Eg: At location [0] there should be property with id 16 (since its ref_no is greater) and at [1] property with id 10.

$array = [
    [
        'Property' => ['id' => 10, 'member_id' => 2, 'ref_no' => 333]
    ],
    [
        'Property' => ['id' => 16, 'member_id' => 4, 'ref_no' => 509]
    ]
];

The size of the main array is dynamic and that of Property array remains same.


Solution

  • apply usort

      usort($input, function ($a, $b) {return ($a['ref_no']>$b['ref_no']);});