bouton Posted September 20, 2006 Share Posted September 20, 2006 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 JulySmith 104 310Brown 316 0Doe 0 36which is a listing likebegin graph_user_hpcx_last_name:SmithBrownDoeend graph_user_hpcx_last_name:begin graph_user_hpcx_Accounting_Month = 6June1043160end_graph_user_hpcx_Accounting_Month = 6begin graph_user_hpcx_Accounting_Month = 7July310036end_graph_user_hpcx_Accounting_Month = 7I'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! Link to comment https://forums.phpfreaks.com/topic/21401-getting-associative-array-to-output-correct-including-missing-value/ Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 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]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/21401-getting-associative-array-to-output-correct-including-missing-value/#findComment-95382 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.