Sort array in PHP by key value (With fields with integer values)

Asked

Viewed 32 times

-4

I have a multidimensional array of type

echo "<pre>";
print_r($order_info);
exit();
Array
(
    [0] => Array
        (
            [order_product_id] => 3227
            [seller_id] => 27
        )
    [1] => Array
        (
            [order_product_id] => 3249
            [seller_id] => 1
        )
    [2] => Array
        (
            [order_product_id] => 3040
            [seller_id] => 27
        )
    [3] => Array
        (
            [order_product_id] => 1240
            [seller_id] => 1
        )

I would like to arrange it ASCENDANTLY by seller_id

that is to say I wish he’d stay the way he is:

Array
(
    [1] => Array
        (
            [order_product_id] => 3249
            [seller_id] => 1
        )
    [3] => Array
        (
            [order_product_id] => 1240
            [seller_id] => 1
        )
    [0] => Array
        (
            [order_product_id] => 3227
            [seller_id] => 27
        )
    [2] => Array
        (
            [order_product_id] => 3040
            [seller_id] => 27
        )

In other words I would like to group them by seller_id (which is not another array)

what I have to do with my array $order_info?

1 answer

0

I think this will solve your problem.

function ordenarPorSellerID($a, $b) { 
    return (($a['seller_id'] != $b['seller_id']) ? (($a['seller_id'] < $b['seller_id']) ? -1 : 1) : 0);
}
  • Instead of the ternary, use the Spaceship is simpler: https://answall.com/a/344648/5878

Browser other questions tagged

You are not signed in. Login or sign up in order to post.