This is a homework question. I'm not asking for the full answer just some tips to keep moving along. I've been working on this for way to long. I'm multiplying the 3 dimensional array contains a list of people and the qty of tools they are buying I'm multiplying the item qty by an array of prices. The problem I'm having is that all three arrays are giving the same total instead of multipying each by a different factor. I've tried a few different approaches. I figured I need to use a while statement or if else to multiply each price to it's $key=>$value, but can't seem to get it to work.
<?php
$names = array(array( 'Name' => 'Tom',
'Hammers' => 3,
'Saws' => 2,
'Drills' => 2
),
array( 'Name' => 'Sue',
'Hammers' => 2,
'Saws' => 3,
'Drills' => 2
),
array( 'Name' => 'Steve',
'Hammers' => 2,
'Saws' => 2,
'Drills' =>2
)
);
$prices = array('Hammers'=>3.00,'Saws'=>2.50,'Drills'=>3.25);
for($array=0;$array<count($names);$array++){
foreach($names[$array] as $key=>$value)
{
$total= $names[0]["Hammers"] * $prices['Hammers'];
$total= $names[1]["Hammers"] * $prices['Hammers'];
$total= $names[2]["Hammers"] * $prices['Hammers'];
echo $key." = ".$value. "total".$total.'<br>';
}
}
?>
The problem is here
$total= $names[0]["Hammers"] * $prices['Hammers'];
$total= $names[1]["Hammers"] * $prices['Hammers'];
$total= $names[2]["Hammers"] * $prices['Hammers'];
echo $key." = ".$value. "total".$total.'<br>';
You don't want the full solution, right? Well, you are just summing the Hammers value. How about you just do a foreach in your $names
and calculate the $total
inside it, using the $prices
array. No nested for
s. There is no need for that.
Hope I was helpful and if you need some code, just comment that I'll edit this answer.
Guess this simple script should do it?
$total_overall = 0.0;
foreach($names as $name) {
$total_hammers = $name["Hammers"] * $prices["Hammers"];
$total_saws = $name["Saws"] * $prices["Saws"];
$total_drills = $name["Drills"] * $prices["Drills"];
// print single prices sums if you want ...
$total_for_person = $total_hammers + $total_saws + $total_drills;
$total_overall += $total_for_person;
echo "Total for person ". $name["Name"]." = ".$total_for_person;
}
// print total overall if you want ...
There are two reasons you're getting the same number repeatedly. First, when you do this:
$total= $names[0]["Hammers"] * $prices['Hammers'];
$total= $names[1]["Hammers"] * $prices['Hammers'];
$total= $names[2]["Hammers"] * $prices['Hammers'];
You're assigning a value to $total
three times. Each time you assign to $total
, you overwrite the previous value. So these three lines have the same effect as:
$total= $names[2]["Hammers"] * $prices['Hammers'];
The second problem is that you're always calculating the total for Hammers, rather than calculating the total for the current type of item.
One thing that you can use to your advantage with this problem is that except for 'Name'
, $prices
has the same keys as each of the arrays in $names
. This means you can calculate each item total for each person using $value * $prices[$key]
, like this:
for ($array=0; $array < count($names); $array++) {
foreach($names[$array] as $key => $value) {
$total = $value * $prices[$key];
echo $key." = ".$value. "total".$total.'<br>';
}
}
The only problem with this is that you'll try to calculate a total for Name
, which obviously doesn't need one, and doesn't have a value in $prices
. That should be fairly easy for you to clean up with an if
, though.