简体   繁体   English

我如何在php中对数组进行排序

[英]How can i sort array in php

Please let me know how can sort array with one column line_total descending format.请让我知道如何使用一列 line_total 降序格式对数组进行排序。 I want that array in line_total descending format bcoz i want to display data upper sale to lower sale.我想要 line_total 降序格式的数组 bcoz 我想显示数据从高到低的销售。 So please help me.所以请帮助我。

Array
(
    [totalusers] => 1
    [TranReport] => Array
        (
            [start] => 01-01-2017
            [end] => 31-12-2017
        )

    [webhits] => 794
    [paypal] => Yes
    [cash] => No
    [Transactions] => Array
        (
            [0] => Array
                (
                    [order_date] => 03-02-2017
                    [customer_name] => Mohsin khan
                    [payment_method] => PayPal
                    [product_list] => Array
                        (
                            [0] => Array
                                (
                                    [product_name] => USB Cable – Iphone → 1M USB Cable - Iphone
                                    [qty] => 1
                                    [line_total] => 9
                                )

                            [1] => Array
                                (
                                    [product_name] => USB Cable – Iphone → 2M USB Cable - Iphone
                                    [qty] => 2
                                    [line_total] => 24
                                )

                        )

                    [quantity] => 3
                    [order_currency] => USD
                    [order_total] => 48.00$
                    [new_total] => 48.00
                )

            [1] => Array
                (
                    [order_date] => 09-01-2017
                    [customer_name] => Mohsin khan
                    [payment_method] => PayPal
                    [product_list] => Array
                        (
                            [0] => Array
                                (
                                    [product_name] => AA USB Charger
                                    [qty] => 1
                                    [line_total] => 15
                                )

                            [1] => Array
                                (
                                    [product_name] => Car Charger - Dual USB - Low Profile
                                    [qty] => 1
                                    [line_total] => 15
                                )

                            [2] => Array
                                (
                                    [product_name] => Mister Hose → 20m Mister Hose
                                    [qty] => 1
                                    [line_total] => 20
                                )

                        )

                    [quantity] => 3
                    [order_currency] => USD
                    [order_total] => 50.00$
                    [new_total] => 50.00
                )

            [2] => Array
                (
                    [order_date] => 07-01-2017
                    [customer_name] => Mohsin khan
                    [payment_method] => PayPal
                    [product_list] => Array
                        (
                            [0] => Array
                                (
                                    [product_name] => Quick Charge V3 - Dual USB -  Car Charger
                                    [qty] => 1
                                    [line_total] => 15
                                )

                            [1] => Array
                                (
                                    [product_name] => Car Charger - Dual USB - Low Profile
                                    [qty] => 1
                                    [line_total] => 15
                                )

                            [2] => Array
                                (
                                    [product_name] => Mister Hose → 20m Mister Hose
                                    [qty] => 1
                                    [line_total] => 20
                                )

                        )

                    [quantity] => 1
                    [order_currency] => USD
                    [order_total] => 15.00$
                    [new_total] => 15.00
                )

        )

    [deliveytotal] => 0
)

You need to use usort function http://php.net/manual/en/function.uksort.php .您需要使用usort函数http://php.net/manual/en/function.uksort.php The rest is just my interpretation of what you want to achieve - sort transactions by sum of line_total in products其余的只是我对您想要实现的目标的解释 - 按产品中的line_total总和对交易进行排序

$array = []; //your array

function sumLinesTotal($product_list) {
    return array_sum(array_map($product_list, function($product){
        return $product['line_total'];
    });
}

usort($array['Transactions'], function ($a, $b) {
    return sumLinesTotal($a) < sumLinesTotal($b);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM