I'm creating the array by parsing a SQL query. The database contains one or more entries for each defect type depending on the date range selected. Once I pull the basic query I sort through it and create the new query to count up the defects for each. Below is the code that creates the query.
$totals = array(); //create array to hold totals
$deptScrap = 0; //total scrap for department
//$partScrap = 0; //total scrap for partnumber
while ($row = mysql_fetch_array($dbResult, MYSQL_ASSOC)) {
if(!isset($totals[$row['Part']])) { //search array to see if current part number is in it
$totals[$row['Part']] = array(); //create array to store part info
}
if(!isset($totals[$row['Part']][$row['Type']])) {
$totals[$row['Part']][$row['Type']] = 0; //create running total of defects per defect type and set to zero
$totals[$row['Part']][$row['Type']] += $row['Defects'];
$deptScrap += $row['Defects']; //add to running total of total defects for department
} else {
$totals[$row['Part']][$row['Type']] += $row['Defects']; //Add the number of defects per defect type to running total
$deptScrap += $row['Defects']; //add to running total of total defects for department
}
}
So I could maybe push another element on the end of this array with the total defects as the value? As you can see I am already totaling the defects so thats already there. Then when I sorted the whole array I would sort on the last element in each of the second array? Guess I am not sure exactly how to implement this, if this is the right track if the first place.
Or would I push the total defects onto the end of the first array as another element value and then sort on that? Would I do a uasort and pass to a function that takes 3 variables and then sort on $a & $c, the first and last elements in the array?