1

I have a multidimensional array in php and I want to sort it according to the entered time.

Array
(
    [0] => Array
        (
            [account_id] => 9
            [entered] => 1369374812
        )

    [1] => Array
        (
            [account_id] => 9
            [entered] => 1377587453
        )

    [2] => Array
        (
            [account_id] => 9
            [entered] => 1373542381
        )

    [3] => Array
        (
            [account_id] => 9
            [entered] => 1372988725
        )

    [4] => Array
        (
            [account_id] => 353
            [entered] => 1380191316
        )

    [5] => Array
        (
            [account_id] => 9
            [entered] => 1377587610
        )
)
1
  • 1
    Same as all the other thousands of array sorting questions: either usort() or array_multisort() Commented Sep 27, 2013 at 19:56

3 Answers 3

1

You can do that with array_multisort

//in PHP 5.5:
$rgOrder = array_column($rgData, 'entered');
array_multisort($rgOrder, SORT_ASC, $rgData);

if you have PHP older than 5.5, then:

$rgOrder = array_map(function($rgItem)
{
   return $rgItem['entered'];
}, $rgData);
array_multisort($rgOrder, SORT_ASC, $rgData);

-you can find a fiddle here. If you don't want to use array_multisort (since it requires to create temporary array first), you can act like:

usort($rgData, function($rgX, $rgY)
{
   return $rgX['entered']>$rgY['entered']?-1:$rgX['entered']!=$rgY['entered'];
});

-here's fiddle for it. All samples require at least PHP 5.3. Otherwise you need to use create_function for callback definitions.

Sign up to request clarification or add additional context in comments.

5 Comments

hi Alma my array is $finalArray and i want to sort it in DESC order but i can't do this using your code please help me. and my PHP version is 5.2.9 thanks
Then may be it's array_multisort($rgOrder, SORT_DESC, $finalArray); ? ($rgOrder is generated same way)
sorry Alma it is not working i replaced $rgData to $finalArray but its not working
@sandy - strange. Added fiddle. Anyway, you could try usort option.
Be aware that the second example will only work in 5.3+. If you run less then that you need to use a function name instead of an anonymous function.
0

Try this,

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($your_array,"account_id");

2 Comments

thanks rohan its work! i want to sort it 'DESC' order, i try to change the sorting order but it not working proper after change the order.
Use asort(array_reverse($sorter)); in place of asort($sorter); and try
0

You can achieve the sorting using usort

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);

function compare($a, $b) {
        if ( $a['entered'] == $b['entered'] ) {
                return 0;
        }

        return ( $a['entered'] < $b['entered'] ) ? -1 : 1;
}

$a = array(
        array('account_id' => 9, 'entered' => 1369374812),
        array('account_id' => 9, 'entered' => 1377587453),
        array('account_id' => 9, 'entered' => 1373542381)
);

usort($a, "compare");

print_r($a);
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.