I have two multi-dimensional arrays - $taste_observations
as the name suggests is a list of tastes recorded as our tasters test products. Each observation has the tester's name, the name of the taste (e.g. sweet, spicy etc) a "stage" when the observation was made and an intensity of the taste.
There are a finite number of tastes and I'm trying to summarize the observations into another array $taste_summary
with one entry for each taste, capturing the name of the taste, the count of how many times it was observed, the maximum intensity and the sum of the intensities so I can calculate the avg intensity.
There's something wrong with my nested foreach loops though, as I always wind up with 0 for all accumulated values. Here's the code I'm using, with simplified sample data:
// Capture the list of taste observations across analyses and phases of analysis
$taste_observations = array();
// Temporary test data for Stack Overflow Question
$taste_observations[] = [
'analyst' => 'Ken',
'phase' => 'Entrance',
'taste' => 'Sweet',
'intensity' => 1
];
$taste_observations[] = [
'analyst' => 'Ken',
'phase' => 'Entrance',
'taste' => 'Spicy',
'intensity' => 1
];
$taste_observations[] = [
'analyst' => 'Ken',
'phase' => 'Center',
'taste' => 'Sweet',
'intensity' => 2
];
$taste_observations[] = [
'analyst' => 'Bob',
'phase' => 'Entrance',
'taste' => 'Savory',
'intensity' => 1
];
$taste_observations[] = [
'analyst' => 'Bob',
'phase' => 'After',
'taste' => 'Sweet',
];
// Sort the list of taste observations by taste ASC
$keys = array_column($taste_observations, 'taste');
array_multisort($keys, SORT_ASC, $taste_observations);
// Initialize the taste summary array
$taste_summary = array(
array(
'taste' => 'Savory',
'count' => 0,
'frequency' => 0,
'max' => 0,
'sum' => 0,
),
array(
'taste' => 'Spicy',
'count' => 0,
'frequency' => 0,
'max' => 0,
'sum' => 0,
),
array(
'taste' => 'Sweet',
'count' => 0,
'frequency' => 0,
'max' => 0,
'sum' => 0,
),
);
// Summarize the frequency, maximum and avg intensity for each taste from the analyses
foreach ($taste_summary as $k1 => $ts) {
foreach ($taste_observations as $k2 => $to) {
if ($to['taste'] == $ts['taste']) {
echo("Summary taste = ".$ts['taste']." while Observed taste = ".$to['taste']." and Observed intensity = ".$to['intensity']."<br>");
$ts['frequency'] = $ts['frequency'] + 1;
echo("Incrementing Frequency for Taste ".$ts['taste']." to ".$ts['frequency']."<br>");
if ($to['intensity'] <> 0) {
$ts['count'] = $ts['count'] + 1;
echo("Incrementing Summary Count for Taste ".$ts['taste']." to ".$ts['count']."<br>");
$ts['sum'] = $ts['sum'] + $to['intensity'];
echo("Increasing Sum for Taste ".$ts['taste']." to ".$ts['sum']."<br>");
if ($to['intensity'] > $ts['max']) {
$ts['max'] = $to['intensity'];
echo("Changing Max for Taste ".$ts['taste']." to ".$ts['max']."<br>");
}
}
}
}
}
print_r($taste_summary);
Here are the results I get - note the echo statements are showing that while the loops are executing it appears I am correctly accumulating the count, sum & max, but at the end of it all i still have zero values in my $taste_summary
array:
Summary taste = Savory while Observed taste = Savory and Observed intensity = 1
Incrementing Frequency for Taste Savory to 1
Incrementing Summary Count for Taste Savory to 1
Increasing Sum for Taste Savory to 1
Changing Max for Taste Savory to 1
Summary taste = Spicy while Observed taste = Spicy and Observed intensity = 1
Incrementing Frequency for Taste Spicy to 1
Incrementing Summary Count for Taste Spicy to 1
Increasing Sum for Taste Spicy to 1
Changing Max for Taste Spicy to 1
Summary taste = Sweet while Observed taste = Sweet and Observed intensity =
Incrementing Frequency for Taste Sweet to 1
Summary taste = Sweet while Observed taste = Sweet and Observed intensity = 2
Incrementing Frequency for Taste Sweet to 2
Incrementing Summary Count for Taste Sweet to 1
Increasing Sum for Taste Sweet to 2
Changing Max for Taste Sweet to 2
Summary taste = Sweet while Observed taste = Sweet and Observed intensity = 1
Incrementing Frequency for Taste Sweet to 3
Incrementing Summary Count for Taste Sweet to 2
Increasing Sum for Taste Sweet to 3
Array ( [0] => Array ( [taste] => Bitter [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [1] => Array ( [taste] => Creamy / Oily [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [2] => Array ( [taste] => Dry / Tannic [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [3] => Array ( [taste] => Salty [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [4] => Array ( [taste] => Savory [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [5] => Array ( [taste] => Sour [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [6] => Array ( [taste] => Spicy [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) [7] => Array ( [taste] => Sweet [count] => 0 [frequency] => 0 [max] => 0 [sum] => 0 ) )