如何在php中对对象的集合进行排序

时间:2017-08-18 14:16:15

标签: php

请帮我在PHP中对这种类型的集合进行排序,下面是对象的集合,必须按照严格的顺序排序。

$og = stdClass Object
(
    [datanumen[DOT]com] => 10
    [diskinternals[DOT]com] => 1
    [download[DOT]cnet[DOT]com] => 2
    [easeus[DOT]com] => 5
    [en[DOT]pstrecovery[DOT]net] => 6
    [en[DOT]pstrecovery[DOT]org] => 3
    [howto-outlook[DOT]com] => 4
    [kerneldatarecovery[DOT]com] => 2
    [mail2web[DOT]com] => 2
    [msoutlook[DOT]info] => 4
    [nirsoft[DOT]net] => 1
    [office[DOT]microsoft[DOT]com] => 14
    [officerecovery[DOT]com] => 1
    [outlook[DOT]recoverytoolbox[DOT]com] => 1
    [outlookpstrecovery[DOT]com] => 16
    [outlookrecovery-tool[DOT]com] => 1
    [outlookrecovery[DOT]net] => 1
    [pst-repair-tool-free[DOT]software[DOT]informer[DOT]com] => 1
    [pstrepair[DOT]net] => 1
    [pstrepairutility[DOT]net] => 9
    [pstscanner[DOT]com] => 11
    [recovermyemail[DOT]com] => 2
    [slipstick[DOT]com] => 4
    [social[DOT]technet[DOT]microsoft[DOT]com] => 2
    [stellarinfo[DOT]com] => 14
    [support[DOT]microsoft[DOT]com] => 15
    [techrepublic[DOT]com] => 17
)

请帮我在php中对这类收集进行排序

1 个答案:

答案 0 :(得分:2)

如果您暂时转换为数组,这很容易:

$og = (array) $og;
asort( $og );
$og = (object) $og;

举个例子:

$og = new stdClass;
$og->a = 5;
$og->b = 2;
$og->c = 8;
$og->d = 1;

$og = (array) $og;
asort( $og );
$og = (object) $og;

echo '<pre>';
print_r( $og );
echo '</pre>';