0

Hi i have an array which is combinations of 2 separate array producing same result, following are my array after merge :

Array
(
    [0] => Array
        (
            [event_id] => 16
            [user_id] => 3
            [date_created] => 20130227095010

        )

    [1] => Array
        (
            [event_id] => 15
            [user_id] => 2
            [date_created] => 20130227101734

        )

    [2] => Array
        (
            [event_id] => 16
            [user_id] => 2
            [date_created] => 20130227094856

        )

)

From here onwards how can i sort again based on date_created desc or asc without using previously 2 arrays which i used to merge?

Update: previous array result

array1:

Array
(
    [0] => Array
        (
            [event_id] => 15
            [user_id] => 2
            [date_created] => 20130227101734


        )

    [1] => Array
        (
            [event_id] => 16
            [user_id] => 2
            [date_created] => 20130227094856

        )

)

array 2:

Array
(
    [0] => Array
        (
            [event_id] => 16
            [user_id] => 3
            [date_created] => 20130227095010

        )

)

How can i use loop within these function to extract date_created for each of above arrays

usort($array,function($a, $b) { return $a['date_created'] - $b['date_created']; }); 
1
  • array_map is used to apply a function to each element of the array, not sorting the array itself Commented Feb 27, 2013 at 2:32

3 Answers 3

2

Very easy with something like usort().

usort($array,
      function($a, $b) { return $a['date_created'] - $b['date_created']; });
Sign up to request clarification or add additional context in comments.

5 Comments

The date_created key of $a and $b should be compared.
@Matthew Whoops, I missed that somehow :)
@alex is there away to sort after merge without using previously 2 arrays used to merge?
@zlippr I'm not using two arrays.
@alex how can i use loop to get $a['date_created'] - $b['date_created'] since for me it looks like this $a[$i]['date_created'] - $b[$i]['date_created'] ?
1

You can create your own sort function callback and use it with the usort method. There is need to re-use your first 2 arrays.

usort($yourMergedArray, yourCallBackSort);

and

function yourCallBackSort($firstEntry, $secondEntry)
{
    // Do some stuff to compare your values and return -1, 0 or 1
    // depend if $firstEntry id <, = or > to $secondEntry
}

Comments

0

Try this,

$sort = array();
foreach($your_combined_array as $k=>$v) {
    $sort['date_created'][$k] = $v['date_created'];
}

array_multisort($sort['date_created'], SORT_DESC, $your_combined_array);


echo "<pre>";
print_r($your_combined_array);

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.