2

I know this would be an easy one but I don't get it. All answered I found on the net was... too complex to me, maybe.

Here is my typical array:

array(
(int) 0 => array(
    'Conversation' => array(
        'id' => '1',
        'created' => '2012-08-04 00:00:00'
    ),
    'ConversationUser' => array(
        (int) 0 => array(
            'id' => '1'
        ),
        (int) 1 => array(
            'id' => '2'
        )
    )
),
(int) 1 => array(
    'Conversation' => array(
        'id' => '2',
        'created' => '2012-08-01 00:00:00'
    ),
    'ConversationUser' => array(
        (int) 0 => array(
            'id' => '1'
        ),
        (int) 1 => array(
            'id' => '2'
        )
    )
));

I want to sort my data with ['Conversation']['created'] date, asc or desc.

Any simple answer ? :P

P.S. I can't use MYSQL sort, I retrieve the data one by one and create that array.

3
  • You may want to read up on uasort(). You'll just have to code a little function to do the array access and date comparison then. Commented Aug 5, 2012 at 17:54
  • P.S. I can't use MYSQL sort, I retrieve the data one by one and create that array. The two do not contrast each other. You can use MySQL sort and iterate the results one by one to create the array. Please share your query with us. Commented Aug 5, 2012 at 17:54
  • possible duplicate of How do I sort a multidimensional array in php Commented Aug 5, 2012 at 17:56

4 Answers 4

3

Use usort() :

usort($your_array, function($a, $b){
    $a = strtotime($a['Conversation']['created']);
    $b = strtotime($b['Conversation']['created']);

    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
});
Sign up to request clarification or add additional context in comments.

1 Comment

I believe you can simply return $a - $b, and even skip the temporary variables altogether.
3

You can use array_multisort to do this:

// $data is your array from the example
// first obtain the rows for sorting
$sortkeys = array();
foreach ($data as $row) {
    $sortkeys[] = $row['Conversation']['created'];
}

// sort $data according to $sortkeys
array_multisort($sortkeys, $data);
var_dump($data);

1 Comment

the best and the most universal answer in my opinion
1

You should have a look to uksort() and usort() functions, which let you customize the way arrays are sorted.

Either the solution is simple or complex, remember what Einstein said once: "Things should be always done as simple as possible, but never simpler than they really are".

If you have some trouble with these functions, we can give you further clues ;-)

Comments

0

You can use usort() (or, to maintain index association, uasort(). Example: (assumes your array is $arr):

usort($arr, function($a, $b) {
    return
    preg_replace('/\D/', '', $b['Conversation']['created'])
    >
    preg_replace('/\D/', '', $a['Conversation']['created']);
});

That will sort descending. Change > to < for ascending.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.