【问题标题】:How to sort object array by value deep inside array如何按数组深处的值对对象数组进行排序
【发布时间】:2014-07-14 21:21:04
【问题描述】:

我有这个数组:

stdClass Object
(
    [tid] => 26001835
    [vid] => 5
    [name] => AppleTV
    [description] => My description
    [format] => filtered_html
    [weight] => 0
    [vocabulary_machine_name] => how_to_watch_device
    [field_device_image] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [fid] => 26608990
                            [alt] => 
                            [title] => 
                            [width] => 194
                            [height] => 102
                            [uid] => 26000697
                            [filename] => Apple-TV.png
                            [uri] => public://Apple-TV.png
                            [filemime] => image/png
                            [filesize] => 2103
                            [status] => 1
                            [timestamp] => 1405346182
                        )

                )

        )

    [field_buy_now_button_link] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => http://www.something.com
                            [format] => 
                            [safe_value] => http://www.something.com
                        )

                )

        )

    [field_learn_more] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => http://something.com/somepage
                            [format] => 
                            [safe_value] => http://something.com/somepage
                        )

                )

        )

    [field_device_category] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => network
                        )

                )

        )

)
stdClass Object
(
    [tid] => 26001834
    [vid] => 5
    [name] => Playstation - USA
    [description] => My description
    [format] => filtered_html
    [weight] => 2
    [vocabulary_machine_name] => how_to_watch_device
    [field_device_image] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [fid] => 26608991
                            [alt] => 
                            [title] => 
                            [width] => 194
                            [height] => 102
                            [uid] => 26000697
                            [filename] => ps4network.png
                            [uri] => public://ps4network.png
                            [filemime] => image/png
                            [filesize] => 4566
                            [status] => 1
                            [timestamp] => 1405346218
                        )

                )

        )

    [field_buy_now_button_link] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => http://www.somesite.com
                            [format] => 
                            [safe_value] => http://somesite.com
                        )

                )

        )

    [field_learn_more] => Array
        (
        )

    [field_device_category] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => blast_areas
                        )

                )

        )

)
stdClass Object
(
    [tid] => 26001836
    [vid] => 5
    [name] => Brighthouse Networks
    [description] => My description
    [format] => filtered_html
    [weight] => 3
    [vocabulary_machine_name] => how_to_watch_device
    [field_device_image] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [fid] => 26608993
                            [alt] => 
                            [title] => 
                            [width] => 194
                            [height] => 102
                            [uid] => 26000697
                            [filename] => brighthouse.png
                            [uri] => public://brighthouse.png
                            [filemime] => image/png
                            [filesize] => 8392
                            [status] => 1
                            [timestamp] => 1405358781
                        )

                )

        )

    [field_buy_now_button_link] => Array
        (
        )

    [field_learn_more] => Array
        (
        )

    [field_device_category] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => ppv_provider
                        )

                )

        )

)

我想按 field_device_category 中的值对数组进行排序。基本上,我想对结果进行分组,但首先我需要确保所有对象都按 field_device_category 排序。

提前致谢!

【问题讨论】:

    标签: php arrays sorting drupal drupal-7


    【解决方案1】:

    使用usort 进行这种排序。它在后台使用快速排序,并采用用户定义的函数来比较数组元素:

    usort($array, "complicatedArrayComparer");
    function complicatedArrayComparer($a,$b)
    {
        if ($a['field_device_category'] == $b['field_device_category']) {
            return 0;
        }
        return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
    }
    

    【讨论】:

    • 有趣。 ['field_device_category'] 的条目需要定位到 ['field_device_category'] 在数组中出现的位置,对吗?类似于 ->field_device_category['und'][0]['value'],对吧?
    • 没错。您只需将数组元素作为函数的参数。您唯一要做的就是比较哪个数组元素更大并返回 1(第一个更大)、0(等于)或 -1(第二个)。
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2021-12-20
    • 2013-02-24
    • 2021-08-10
    • 2015-08-28
    相关资源
    最近更新 更多