php - Group multidimensional array
Given the following array:
var dates = ['2012-10-01', '2012-10-02', '2012-10-03', '2011-01-01', '2011-01-02', '2011-06-03', '2010-09-01', '2010-09-02', '2010-08-22' ];
I need it grouped by year then month then day so that my final result is something like this:
Array (
"2012" =>
Array ( "10" => Array ["01", "02", "03"] )
"2011" =>
Array ( "01" => Array ["01", "02"] )
Array ( "06" => Array ["03"] )
"2010" =>
Array ( "09" => Array ["01", "02"] )
Array ( "08" => Array ["22"] )
)
In the end I will auto populate a MM/DD/YYYY drop downs. So if a user selects year 2012 the following month drop down value is only 10 followed by the day drop down which would only have 01, 02, 03. If they select 2011 the following month drop down would only say 01, 06. If they select 01 then the following days would only be 01 or 02.
Hope that didnt confuse anyone and the question is clear. Multidimensional arrays are the worst I know.
Ideally Id like to do this in Javascript but PHP is ok too. One array in, one array out.
Answer
Solution:
This would do it:
Output:
Answer
Solution:
Use Underscore.js
Example:
Answer
Solution:
First, you parse all the date strings out in to their separate parts. Then, you iterate through those parts, and put them in to the appropriate arrays.
(A very general answer for a very general question.)