如何按值深入数组内的值排序对象数组

时间:2014-07-14 21:21:05

标签: php arrays sorting drupal drupal-7

我有这个数组:

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排序。

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用usort进行此类排序。它在后台使用Quicksort并使用用户定义的函数来比较数组元素:

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