使用 stdClass 对象对数组进行排序

发布于 2024-12-15 15:55:48 字数 6220 浏览 0 评论 0原文

我正在尝试按 Ubercart order.tpl.php 文件中每个 stdClass 对象的 [title] 对该数组进行排序。我尝试过进行正常排序,但由于它们都是 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>

I am attempting sort this array by each stdClass Object's [title] in an Ubercart order.tpl.php file. I have tried doing just a normal sort but since they all are stdClass Objects it doesn't do anything.

<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>

This is the code controlling it in the order.tpl.php

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-12-22 15:55:48

我认为 usort() 应该可以工作:

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

usort($theArray, 'sortByTitle');

您可能需要进行一些检查以确保 title 成员确实存在。

I think usort() should work:

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

usort($theArray, 'sortByTitle');

You may want to throw in some checking to make sure the title member actually exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文