3

我有一个数组,我想根据创建的字段对其进行排序,我试图通过一些代码对其进行排序,但我不能这样做,所以请帮助我,为此我有一些代码我的代码不起作用......,

Array 
(
[0] => stdClass Object
    (
        [status_id] => 26
        [message] => this is test
        [created] => 2013-03-05 23:11:54
        [uid] => 1
        [first_name] => sandeep
        [last_name] => singh
    )

[1] => stdClass Object
    (
        [status_id] => 27
        [message] => this is sisfd
        [created] => 2013-09-10 22:28:46
        [uid] => 1
        [first_name] => sandeep
        [last_name] => singh
    )

[2] => stdClass Object
    (
        [status_id] => 28
        [message] => nvcbncvnbcvnbvcnb
        [created] => 2013-09-25 22:29:12
        [uid] => 1
        [first_name] => sandeep
        [last_name] => singh
    )

[3] => stdClass Object
    (
        [status_id] => 29
        [message] => this is time test
        [created] => 2013-09-10 23:08:52
        [uid] => 1
        [first_name] => sandeep
        [last_name] => singh
    )

[4] => stdClass Object
    (
        [status_id] => 30
        [message] => thuiso9safdasdfasdfasdfasdf
        [created] => 2013-09-26 00:41:15
        [uid] => 1
        [first_name] => sandeep
        [last_name] => singh
    )

)

4

3 回答 3

0

尝试这个

 $yourarray = array(array(...), array(....),.....);

 function myFieldSort($a, $b)
 {
   return $b['created'] - $a['created'];
 }

 usort($yourarray, "myFieldSort");
于 2013-09-27T19:45:09.880 回答
0
function array_multi_subsort($array, $subkey)
{
    $b = array(); $c = array();

    foreach ($array as $k => $v)
    {
        $b[$k] = strtolower($v[$subkey]);
    }

    asort($b);
    foreach ($b as $key => $val)
    {
        $c[] = $array[$key];
    }

    return $c;
}

// sorting through "created" element in the arrays
$data = array_multi_subsort($array, 'created');
print_r($data);

还可以参考 PHP.net 手册:
http://php.net/manual/en/function.array-multisort.php

于 2013-09-27T19:46:14.307 回答
0

请试试这个:

function array_multi_subsort($array, $subkey)
{
    $b = array(); $c = array();

    foreach ($array as $k => $v)
    {
        $b[$k] = strtolower($v->$subkey);
    }

    asort($b);
    foreach ($b as $key => $val)
    {
        $c[] = $array[$key];
    }

    return $c;
}

// sorting through "created" element in the arrays
$data = array_multi_subsort($array, 'created');
print_r($data);
于 2016-03-17T10:26:44.870 回答