Jump to content

getting associative array to output correct including missing value


Recommended Posts

I have an associative array that looks like
[code]
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
        )

[/code]

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
[code]
$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);     
[/code]

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)

[code]
    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";
              }
            }
        }
    }
[/code]

Any suggestions on how best to get this? Been reading all I can and am still stumped!
I think the correct syntax for the last name array should be changed from:

[code=php:0]$last_name_array[]=$value[graph_user_hpcx_last_name];[/code]

to
[code=php:0]$last_name_array[]=$results[$key][graph_user_hpcx_last_name];[/code]


Regards
Huggie
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.