3

i have a array and i want to sort it according to created field, i tried to sort it by some code but i can't do this so please help me, for this i have some code my code is not working... ,

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
    )

)

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
SaNdY
  • 518
  • 4
  • 14

3 Answers3

0

Try this

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

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

 usort($yourarray, "myFieldSort");
Nitish Kumar
  • 4,850
  • 3
  • 20
  • 38
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);

Also reference on PHP.net manual:
http://php.net/manual/en/function.array-multisort.php

tfont
  • 10,891
  • 7
  • 56
  • 52
0

Please try this :

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);
gagan mahatma
  • 336
  • 2
  • 9