จัดเรียง Multi Array ตามค่า Value

วิธีการจัดเรียง ค่าใน array

ก่อนจัด

$new = [
    [
        'name' => 'Flower',
        'order' => 3,
    ],
    [
        'name' => 'Free',
        'order' => 2,
    ],
    [
        'name' => 'Ready',
        'order' => 1,
    ],
];
$keys = array_column($new, 'order');
array_multisort($keys, SORT_ASC, $new);
var_dump($new);

หลังจัด

Array
(
    [0] => Array
        (
            [name] => Ready
            [order] => 1
        )
    [1] => Array
        (
            [name] => Free
            [order] => 2
        )
    [2] => Array
        (
            [name] => Flower
            [order] => 3
        )
)

Leave a Reply

Your email address will not be published. Required fields are marked *