0

I'm trying to sort this array by the date but the array is merged and has two different types of date.

[0] => Object
        [Something] => hey
        [date]=>2010-01-03
[1] => Object
        [something] => heyagain
        [somethingelse] => heythere
        [posted_date] => 2011-08-22

I want this array to sort the whole array by date and posted date but the array comes out in order as:

Array1=>(date1,date2,date3) Array2=>(date1,date2,date3)

For instance in (Array2,date2) may be before (Array1,date1) but it does not sort that way. I want to see

Output=>Array2(date1),Array2(date2),Array1(date1),Array2(date3),Array1(date2),Array1(date3

I have tried array_multisort($merge, SORT_NUMERIC, $arg, 'posted_date', SORT_DESC, 'date', SORT_DESC) and a few other but I can't get it to work. I hope this isn't confusing anyone.

2
  • I'd look at array_walk() and array_map() Commented Jan 3, 2012 at 18:08
  • Which dates do date# values in your arrays relate to? What do you mean by two different 'types' of dates? Commented Feb 18 at 16:27

1 Answer 1

1

To use array_multisort, you need to create multiple arrays first, e.g. accessing properties that you want to sort over and wrapping those into another array.

The manual page gives good examples how to do this or that. However strings just won't work:

array_multisort($merge, SORT_NUMERIC, $arg, 'posted_date', SORT_DESC, 'date', SORT_DESC);

See as well: Sort data of Php array by values of another array

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

3 Comments

Multisort will sort the arrays how i like them but my outcome is still in order starting with Array1 then on to Array2. They have to be in the same array and then sorted to mix them but order by date.
[0] => Object has no posted_date, [1] => Object has no date. I wonder if you want to sort in this case, you dropped that part in your question.
[0]=>Object does not have a posted date. Array1 had 'posted_date' and Array2 has 'date'. These were merged so it has different key names for dates. I tried selecting the 'posted_date' AS 'date' from the db but zend wouldn't have it or most likely my error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.