Jump to content


Photo

getting associative array to output correct including missing value


  • Please log in to reply
1 reply to this topic

#1 bouton

bouton
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 September 2006 - 01:20 PM

I have an associative array that looks like
Array
(
    [0] => Array
        (
            [graph_user_hpcx_last_name] => Smith
            [graph_user_hpcx_Accounting_Year] => 2006
            [graph_user_hpcx_Accounting_Month] => 6
            [graph_user_hpcx_AllocationUnits] => 104
        )

    [1] => Array
        (
            [graph_user_hpcx_last_name] => Brown
            [graph_user_hpcx_Accounting_Year] => 2006
            [graph_user_hpcx_Accounting_Month] => 6
            [graph_user_hpcx_AllocationUnits] => 316
        )
    [2] => Array
        (
            [graph_user_hpcx_last_name] => Smith
            [graph_user_hpcx_Accounting_Year] => 2006
            [graph_user_hpcx_Accounting_Month] => 7
            [graph_user_hpcx_AllocationUnits] => 310
        )
    [3] => Array
        (
            [graph_user_hpcx_last_name] => Doe
            [graph_user_hpcx_Accounting_Year] => 2006
            [graph_user_hpcx_Accounting_Month] => 7
            [graph_user_hpcx_AllocationUnits] => 36
        )


What I'm trying to do is come up with a loop (to use in a graphing program XML-SWF Charts) which will produce a table like
            June    July
Smith    104      310
Brown    316          0
Doe        0          36

which is a listing like

begin graph_user_hpcx_last_name:
Smith
Brown
Doe
end graph_user_hpcx_last_name:

begin graph_user_hpcx_Accounting_Month = 6
June
104
316
0
end_graph_user_hpcx_Accounting_Month = 6

begin graph_user_hpcx_Accounting_Month = 7
July
310
0
36
end_graph_user_hpcx_Accounting_Month = 7
I've tried to make an array of all the last names
$last_name_array = array();
foreach ($results as $key => $value) {        
	$last_name_array[]=$value[graph_user_hpcx_last_name];
}
sort($last_name_array);
$last_name_array=array_unique($last_name_array);      

then looping through all my results to see if it matches a name, if so print the units, if not print 0 but I get way two sets for each name (cause of the 6 and 7 month)

    foreach ($last_name_array as $key => $value) {        	
         foreach ($results as $key => $value1) {   
             if  ($value == $value1[graph_user_hpcx_last_name])  {
                if ($value1[graph_user_hpcx_Accounting_Year]== 2006 && $value1[graph_user_hpcx_Accounting_Month] == 6) {
         	 echo "$value1[graph_user_hpcx_AllocationUnits]<br />\n";
                }  else {
                echo "0";
               }
             }
         }
    }

Any suggestions on how best to get this? Been reading all I can and am still stumped!


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 03:02 PM

I think the correct syntax for the last name array should be changed from:

$last_name_array[]=$value[graph_user_hpcx_last_name];

to
$last_name_array[]=$results[$key][graph_user_hpcx_last_name];


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users