0

Guys this is mysql tables output. It show for month(Value) get count(Total) for a particular disease. Now I have a php array() of 12 months and I want to compare with that array and find that if for particular month their is no data returned from table add 0 for that month in other new array() of 12 months or else the value found from table.

Value | dname      | Total            
5     | Root Canal | 1         
8     | Root Canal | 1     

my code in php

foreach($ResultArray2[1] as $key => $val){       
if(empty($row_d['Value'])){
$ResultArray2[$i][$key] = '0';
else
$ResultArray2[$i][$row_d['Value']] = $row_d['Total'];

This is what i get

     Array (
[1] => Array
    (
        [1] => 0
        [2] => 0
        [3] => 0
        [4] => 0
        [5] => 0
        [6] => 0
        [7] => 0
        [8] => 0
        [9] => 0
        [10] => 0
        [11] => 0
        [12] => 0
    )

[2] => Array
    (
        [5] => 1
        [8] => 1
    )

)

this is what i need Array (

[1] => Array
    (
        [1] => 0
        [2] => 0
        [3] => 0
        [4] => 0
        [5] => 0
        [6] => 0
        [7] => 0
        [8] => 0
        [9] => 0
        [10] => 0
        [11] => 0
        [12] => 0
    )

[2] => Array
    (

      [1] => 0
        [2] => 0
        [3] => 0
        [4] => 0
        [5] => 1
        [6] => 0
        [7] => 0
        [8] => 1
        [9] => 0
        [10] => 0
        [11] => 0
        [12] => 0
    )

)

8
  • where is month or date column in your table for comparing? Commented Jul 25, 2012 at 7:16
  • What does that $i do? You wanna generate an array which will have 12 indexes each mapped to the count of disease in that month right? Commented Jul 25, 2012 at 7:24
  • here $i is no. of disease..i have 5 types of disease..so it will generate 5 different arrays each having its own month & count in that array. Commented Jul 25, 2012 at 7:26
  • What format is your row data in? I assumed you'd just be querying a table... Commented Jul 25, 2012 at 7:30
  • Never mind, ignore me; I misread it as $ResultArray2 being the result of the query, but it's actually your output. Commented Jul 25, 2012 at 7:45

2 Answers 2

1

If I'm understanding your question correct then

$finalResult = array();
while($row = mysql_fetch_object($querResult)) {
   if(!is_array($finalResult[$row->dname]) {
      $finalResult[$row->dname] = array_pad(array(), 13, 0);
   } 
   $finalResult[$row->dname][$row->Value] = $row->Total;
}

Basically the code checks if there is already an array exists for a particular disease. If not it creates an array of size 13 [to ignore 0] padded with value 0 and updates the value on the go. Here disease becomes a key of outer array. You can use an map to disease name to integer ID if you wish to. In that case

$finalResult[$map[$row->dname]][$row->value] = $row->Total;
Sign up to request clarification or add additional context in comments.

Comments

0
$counts = array_fill(1,12,0);
while ($row = mysql_fetch_assoc($res)){
  $counts[$row['Value']] = $row['Total'];
}

Edit: Since you seem to want to get all the diseases, it may be easier to do the whole thing as one query, like so:

SELECT name,
SUM(IF(Value=1,1,0) as month1,
SUM(IF(Value=2,1,0) as month2,
SUM(IF(Value=3,1,0) as month3,
...
FROM tablename
GROUP BY name

2 Comments

dude i m having already having array like $ResultArray2[1] = array_fill(1,12,'0');
@KaushilRambhia (having now seen your code edit) so why aren't you doing that for $ResultArray2[$i] as well? Seems like the simplest way. Although given what you want overall I'd go for the SQL approach I edited into my answer.

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.