Php 我的usort代码工作不正常

Php 我的usort代码工作不正常,php,Php,我对该数组排序有问题: Array ( [0] => stdClass Object ( [id] => 120 [registration_date] => 2012-10-19 16:57:46 [username] => Jeff ) [1] => stdClass Object ( [id] =>

我对该数组排序有问题:

Array
(
    [0] => stdClass Object
        (
            [id] => 120
            [registration_date] => 2012-10-19 16:57:46
            [username] => Jeff
        )
    [1] => stdClass Object
        (
            [id] => 121
            [registration_date] => 2012-12-23 16:57:46
            [username] => Peter
        )
    [2] => stdClass Object
        (
            [id] => 122
            [registration_date] => 2012-11-30 16:57:46
            [username] => Susan
        )

)
它存储在变量中:

$unsorted_users
我想在注册日期之前订购此阵列,如下所示:

Array
    (
        [0] => stdClass Object
            (
                [id] => 121
                [registration_date] => 2012-12-23 16:57:46
                [username] => Peter

            )
        [1] => stdClass Object
            (
                [id] => 122
                [registration_date] => 2012-11-30 16:57:46
                [username] => Susan

            )
        [2] => stdClass Object
            (
                [id] => 120
                [registration_date] => 2012-10-19 16:57:46
                [username] => Jeff
            )

    )
<?php

//  setup
$unsorted_users = array();

$t = new stdClass(); 
$t->id = 120; $t->username = 'jeff'; $t->registration_date = '2012-10-19 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 121; $t->username = 'Peter'; $t->registration_date = '2012-12-23 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 122; $t->username = 'Susan'; $t->registration_date = '2012-11-30 16:57:46'; 
$unsorted_users[] = $t;
// end setup


$sorted_users =    // this var is nor relevant as it shoud be allways true
usort($unsorted_users, function($a, $b) {
   return strcmp ($a->registration_date, $b->registration_date) ;
});

echo '<pre>';
print_r($unsorted_users)   // that is in fact ordered!

?>
按注册日期排序,如:

2012-12-23 16:57:46
2012-11-30 16:57:46
2012-10-19 16:57:46
而不是原来的样子:

2012-10-19 16:57:46
2012-12-23 16:57:46
2012-11-30 16:57:46
我正在使用这段代码,但效果不好(print\r($sorted\u users);输出$sorted\u users是“1”。我不知道为什么它是1而不是排序数组。)


有什么建议吗?

usort
正在通过引用处理数组,并返回成功布尔值-因此您的
$sorted\u用户
包含该布尔值。现在应该对原始数组进行排序

usort
正在通过引用处理数组,并返回成功布尔值-因此您的
$sorted\u users
包含该布尔值。现在应该对原始数组进行排序

你不能减去(那个)字符串。如果要比较两个字符串,请按如下方式使用strcmp()函数:

Array
    (
        [0] => stdClass Object
            (
                [id] => 121
                [registration_date] => 2012-12-23 16:57:46
                [username] => Peter

            )
        [1] => stdClass Object
            (
                [id] => 122
                [registration_date] => 2012-11-30 16:57:46
                [username] => Susan

            )
        [2] => stdClass Object
            (
                [id] => 120
                [registration_date] => 2012-10-19 16:57:46
                [username] => Jeff
            )

    )
<?php

//  setup
$unsorted_users = array();

$t = new stdClass(); 
$t->id = 120; $t->username = 'jeff'; $t->registration_date = '2012-10-19 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 121; $t->username = 'Peter'; $t->registration_date = '2012-12-23 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 122; $t->username = 'Susan'; $t->registration_date = '2012-11-30 16:57:46'; 
$unsorted_users[] = $t;
// end setup


$sorted_users =    // this var is nor relevant as it shoud be allways true
usort($unsorted_users, function($a, $b) {
   return strcmp ($a->registration_date, $b->registration_date) ;
});

echo '<pre>';
print_r($unsorted_users)   // that is in fact ordered!

?>

拨弄,拨弄你不能删减字符串。如果要比较两个字符串,请按如下方式使用strcmp()函数:

Array
    (
        [0] => stdClass Object
            (
                [id] => 121
                [registration_date] => 2012-12-23 16:57:46
                [username] => Peter

            )
        [1] => stdClass Object
            (
                [id] => 122
                [registration_date] => 2012-11-30 16:57:46
                [username] => Susan

            )
        [2] => stdClass Object
            (
                [id] => 120
                [registration_date] => 2012-10-19 16:57:46
                [username] => Jeff
            )

    )
<?php

//  setup
$unsorted_users = array();

$t = new stdClass(); 
$t->id = 120; $t->username = 'jeff'; $t->registration_date = '2012-10-19 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 121; $t->username = 'Peter'; $t->registration_date = '2012-12-23 16:57:46'; 
$unsorted_users[] = $t;

$t = new stdClass(); 
$t->id = 122; $t->username = 'Susan'; $t->registration_date = '2012-11-30 16:57:46'; 
$unsorted_users[] = $t;
// end setup


$sorted_users =    // this var is nor relevant as it shoud be allways true
usort($unsorted_users, function($a, $b) {
   return strcmp ($a->registration_date, $b->registration_date) ;
});

echo '<pre>';
print_r($unsorted_users)   // that is in fact ordered!

?>


fiddle

已经有一些有效的答案,但要补充一下:

变量包含在对象中,而不是数组中;您有一个要排序的对象数组,因此应使用:

$a['registration_date']
而不是:


已经有一些有效的答案,但要补充:

变量包含在对象中,而不是数组中;您有一个要排序的对象数组,因此应使用:

$a['registration_date']
而不是:


我得到这个错误:致命错误:无法将stdClass类型的对象用作arrayI得到这个错误:致命错误:无法将stdClass类型的对象用作ArrayTanks,现在,与Luis结合使用,它正在工作,谢谢;)谢谢,现在,与Luis结合,它正在发挥作用,谢谢;)