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

时间:2018-01-03 13:39:11

标签: php

我有这个stdclass对象数组

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [message] => c

        )

    [1] => stdClass Object
        (
            [id] => 2
            [message] => b

        )

    [2] => stdClass Object
        (
            [id] => 1
            [message] => a
        )

    [3] => stdClass Object
        (

            [id] => 4
            [message] => d
        )

)

但我需要根据他们的id值对此进行排序....

1 个答案:

答案 0 :(得分:1)

PHP为此提供了usort函数:usort documentation

$sortedArray = usort($array, function($a, $b) {
        return $a->id - $b->id;
    });