How would i pull the correct value out of this php loop
I have this PHP loop
$开发者_如何学Ctwo_related_total = 0;
$three_related_total = 0;
$four_related_total = 0;
$five_related_total = 0;
$all_values = getRelatedProducts(91)
$arr = array(2, 3, 4, 5);
foreach ($arr as $discount_quantity) {
//do something in here here to get the discounted_price or the price
}
Here is the data in $getRelatedProducts. Basically i need to get the discounted total for each array
For example for the the value 2 I need to set the value of $two_related_total to be 729.0000 and so on...or if there is a better way to get the four values please help me out ....thanks in advance
[0] => Array
(
[product_id] => 180
[price] => 749.0000
[discounted_price] => 729.0000
[cost] => 420.0000
[quantity] => 2
)
[1] => Array
(
[product_id] => 180
[price] => 749.0000
[discounted_price] => 545.0000
[cost] => 420.0000
[quantity] => 3
)
[2] => Array
(
[product_id] => 180
[price] => 749.0000
[discounted_price] => 545.0000
[cost] => 420.0000
[quantity] => 4
)
[3] => Array
(
[product_id] => 180
[price] => 749.0000
[discounted_price] => 545.0000
[cost] => 420.0000
[quantity] => 5
)
)
You could switch your related totals to an array:
$related_totals=array();
$all_values = getRelatedProducts(91)
$arr = array(2, 3, 4, 5);
foreach ($arr as $discount_quantity) {
$related_totals[$discount_quantity]=$all_values[$discount_quantity]['price'];
//do something in here here to get the discounted_price or the price
}
I'm not really understanding your question, as as others point out there's no 729 value, (although there are 749). But this gives you the idea.
I THINK this is what you want:
$discounts = array();
foreach($product_array as $key => $product) {
$discounts[$key] = $product['discounted_price'];
}
echo $discounts[2]; // $545.00
Using an array to hold the discounts is far easier than trying to set up individual variables for each. Otherwise you'll eventually end up with $five_hundred_bajillion_60_kajillion_and_3_discounted_total
as your products array grows.
I think this is what you want, but your question is really unclear.
$arr = array(2, 3, 4, 5);
$totals = array(2=>0, 3=>0, 4=>0, 5=>0);
foreach ( $all_values as $product ){
if (in_array($product['quantity'], $arr)) {
$totals[$products['quantity']] += $product['discounted_price'];
}
}
For the first two lines you could alternatively do:
$arr = range(2,5);
$totals = array_fill_keys($arr, 0);
精彩评论