如何在代码点火器中对多维数组进行排序

时间:2013-09-27 19:35:43

标签: php

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

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
    )

3 个答案:

答案 0 :(得分:0)

试试这个

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

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

 usort($yourarray, "myFieldSort");

答案 1 :(得分: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

答案 2 :(得分: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);