1

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
1
  • what is the value of $final_array["value1"]["2012-10-01"] in your second example? Commented Oct 23, 2012 at 7:25

3 Answers 3

2
foreach ($array1 as $value){
    $final_array["value".$value[1]][$value[0]] = $value[2];
}

with $array1 as your first array

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

Comments

1

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>";

DEMO

8 Comments

This won't work if, as I said, the values of 0,1 are not ordered by date.
@HommerSmith This should work just fine, could you explain more why that wouldn't work?
Yes sorry. Imagine that for the first date, there is no 0 value. In that case I would have to create an entrance like this: $finalarray["value0"][$data[0]]=0
If there is no 0 value for the first date, the "value0" index will still be defined on the array as long as there is at least one entry for 0 values on some day. Your last comment is crucial information. You should include this in the question.
Yes, but it needs to be defined for all dates that there is value 1, and the other way around. So, all dates have to have value0 and value1.
|
0

This will work:

$result = array('value0' => array(), 'value1' => array());
foreach ($array as $value) {
    $result[('value' . $value[1])][$value[0]] = $value[2];
}

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.