PHP排序对象


PHP Sorting Objects

    [sections] => stdClass Object
            (
                [22353] => stdClass Object
                    (
                        [id] => 2
                        [section_start_date] => 1410235200
                    )
                [22354] => stdClass Object
                    (
                        [id] => 1
                        [section_start_date] => 1410235260
                    )
)

如何在PHP中按id对上述对象进行排序,同时保留sections对象的键?例如,我想在22353之上显示22354。因为这些是对象,所以键对我来说就是字符串,但我需要保持它们的完整性。

有些混乱正在发生。这些对象不在数组中。请密切注意section对象

就是这么做的

stdClass Object
(
    [111111] => stdClass Object
        (
           [id] => 2
           [section_start_date] => 1410235200
       )
    [999999] => stdClass Object
       (
            [id] => 1
            [section_start_date] => 1410235260
       )
    [222222] => stdClass Object
        (
            [id] => 1
            [section_start_date] => 1410235300
    )
    [555555] => stdClass Object
        (
            [id] => 1
            [section_start_date] => 1410231160
        )
)

步骤将stdClass转换为数组

$data = json_decode(json_encode($object),true);
ksort($data);
print_r($data);

在保持键索引的同时输出新的排序数组。

Array
(
    [111111] => Array
        (
            [id] => 2
            [section_start_date] => 1410235200
        )
    [222222] => Array
        (
            [id] => 1
            [section_start_date] => 1410235300
        )
    [555555] => Array
        (
            [id] => 1
            [section_start_date] => 1410231160
        )
    [999999] => Array
        (
           [id] => 1
           [section_start_date] => 1410235260
        )
)