簡體   English   中英

使用stdClass對象對數組進行排序

[英]Sorting an array with stdClass Objects

我正在嘗試通過Ubercart order.tpl.php文件中的每個stdClass對象的[標題]對此數組進行排序。 我試過做一個正常的排序,但由於它們都是stdClass對象,所以它什么都不做。

<pre>Array
(
[0] => stdClass Object
    (
        [order_product_id] => 1157
        [order_id] => 142
        [nid] => 38
        [title] => Eatonville Forest Camping Permit
        [manufacturer] => 
        [model] => eatonville-camp-permit
        [qty] => 1
        [cost] => 9.00000
        [price] => 10.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 0
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[1] => stdClass Object
    (
        [order_product_id] => 1158
        [order_id] => 142
        [nid] => 35
        [title] => Eatonville Forest Motorized Recreation Access Permit and Release of Liability
        [manufacturer] => 
        [model] => eatonville-motor-rec-access
        [qty] => 1
        [cost] => 175.00000
        [price] => 175.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[2] => stdClass Object
    (
        [order_product_id] => 1159
        [order_id] => 142
        [nid] => 31
        [title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Families and Release of Liability
        [manufacturer] => 
        [model] => snoqualmie-family-non-motor-rec-access
        [qty] => 1
        [cost] => 150.00000
        [price] => 150.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[3] => stdClass Object
    (
        [order_product_id] => 1160
        [order_id] => 142
        [nid] => 40
        [title] => Snoqualmie Forest Camping Permit
        [manufacturer] => 
        [model] => snoqualmie-camp-permit
        [qty] => 1
        [cost] => 300.00000
        [price] => 300.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[4] => stdClass Object
    (
        [order_product_id] => 1161
        [order_id] => 142
        [nid] => 39
        [title] => White River Forest Camping Permit
        [manufacturer] => 
        [model] => whiteriver-camp-permit
        [qty] => 1
        [cost] => 300.00000
        [price] => 300.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[5] => stdClass Object
    (
        [order_product_id] => 1162
        [order_id] => 142
        [nid] => 30
        [title] => White River Forest Family Non-motorized Recreation Access Permit for Families and Release of Liability
        [manufacturer] => 
        [model] => whiteriver-family-non-motor-rec-access
        [qty] => 1
        [cost] => 150.00000
        [price] => 150.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[6] => stdClass Object
    (
        [order_product_id] => 1163
        [order_id] => 142
        [nid] => 33
        [title] => White River Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
        [manufacturer] => 
        [model] => whiteriver-non-motor-rec-access
        [qty] => 1
        [cost] => 75.00000
        [price] => 75.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )

[7] => stdClass Object
    (
        [order_product_id] => 1164
        [order_id] => 142
        [nid] => 34
        [title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability
        [manufacturer] => 
        [model] => snoqualmie-non-motor-rec-access
        [qty] => 1
        [cost] => 75.00000
        [price] => 75.00000
        [weight] => 0
        [data] => Array
            (
                [attributes] => Array
                    (
                    )

                [shippable] => 1
                [restrict_qty] => 1
                [module] => uc_product
            )

        [order_uid] => 13
    )
)</pre>

這是在order.tpl.php中控制它的代碼

<pre>php if (is_array($order->products)) {
                        $context = array(
                          'revision' => 'formatted',
                          'type' => 'order_product',
                          'subject' => array(
                           'order' => $order,
                          ),
                        );
}
</pre>

我認為usort()應該有效:

function sortByTitle($a, $b){
  return strcmp($a->title, $b->title);
}

usort($theArray, 'sortByTitle');

您可能想要進行一些檢查以確保title成員確實存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM