I have an array coming from a SQL query that follows the following format:
[
["2012-10-01", 1, 3890],
["2012-10-01", 0, 8900],
["2012-09-21", 0, 8900],
["2012-09-21", 1, 8900],
...
]
At the end I want to have the counts for 0s and 1s. So for example, for the date 2012-10-01, I would have that the value 1 have a count 3890 and the value 0 have a count 8900. I want to know how I can iterate this array in order to build a final array like this:
$final_array["value0"]["2012-10-01"] = 3890
$final_array["value1"]["2012-10-01"] = 8900
$final_array["total"]["2012-10-01"] = 12790
Taking into account that it can happen that a date doesn't have a count for value 0 or 1, and the order of value 0 and 1 is not always the same.
So the first example could be like this:
[
["2012-10-01", 1, 3890],
["2012-09-21", 1, 8900],
["2012-09-21", 0, 8900],
...
]
As you can see the first date is lacking of value 0 and the final array would still have to have it like this:
$final_array["value0"]["2012-10-01"] = 3890
$final_array["value1"]["2012-10-01"] = 0
Here is the code. You really should try to work it out yourself first, because this is basic array mapping.
foreach($initialarray as $data){
$finalarray["value".$data[1]][$data[0]]=$data[2];
}
EDIT: Here is code that fulfils your updated requirements:
$initialarray = array(array("2012-10-01", 1, 3890), array("2012-09-21", 1, 8900), array("2012-09-21", 0, 8900));
$finalarray["value0"]=array();
$finalarray["value1"]=array();
foreach($initialarray as $data){
$finalarray["value".$data[1]][$data[0]]=$data[2];
$finalarray["value".round(cos($data[1]*pi()/2))][$data[0]]=$finalarray["value".round(cos($data[1]*pi()/2))][$data[0]]?$finalarray["value".round(cos($data[1]*pi()/2))][$data[0]]:0;
}
echo "<pre>";
var_dump($finalarray);
echo "<pre>";