PHP: Comparing an array, getting largest value and updating -
i have array that's submitted menu , have take largest value of subcategory , use highest price. updates can occur multiple times user adds more items.
user scenario
: user clicks on ingredient, another, another. during each addition, prices updated dynamically.
one of subcategories salad greens
or ingredienttype_id = 1
.
current code:
$prices = $db->get_ingredient_prices($list); $free_items = $db->get_free_ingredient_types($item); foreach($prices $price){ if(array_key_exists($price['ingredienttype_id'], $free_items)){ $qtya = $free_items[$price['ingredienttype_id']]; $qtyb = $customizations[$price['id']]; if($qtya == 0){ $qtyc = $qtyb; } elseif($qtya > $qtyb){ $free_items[$price['ingredienttype_id']] = $qtya - $qtyb; $qtyc = 0; } elseif($qtya < $qtyb){ $free_items[$price['ingredienttype_id']] = 0; $qtyc = $qtyb - $qtya; } else { $free_items[$price['ingredienttype_id']] = 0; $qtyc = 0; } } else { $qtyc = $customizations[$price['id']]; } if ($qty > -1) { $customizations_total = $customizations_total + ($price['price'] * $qtyc); } else { $customizations_total = $customizations_total + $price['price']; } } return number_format($customizations_total,2);
now have create loop within sample code, somehow.
here sample output foreach loop when attempting proper category:
$list = [4, 6, 8, 114, 98]; $prices = $db->get_ingredient_prices($list); foreach ($prices $k => $v) { if ($v['ingredienttype_id'] == '1') { $greens[] = $v; } }
sample output:
array (size=6) [prices] 0 => array (size=8) 'id' => string '6' (length=1) 'name' => string 'spinach' (length=7) 'description' => string '' (length=0) 'price' => string '0.69' (length=1) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1f9655f6d35001d40' (length=24) 'nutritionix_cal' => string '55' (length=2) 1 => array (size=8) 'id' => string '8' (length=1) 'name' => string 'kale' (length=4) 'description' => string 'local' (length=5) 'price' => string '0' (length=1) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1ea63d49335001d48' (length=24) 'nutritionix_cal' => string '71' (length=2) 2 => array (size=8) 'id' => string '4' (length=1) 'name' => string 'bleu cheese' (length=11) 'description' => string '' (length=0) 'price' => string '0.69' (length=4) 'ingredienttype_id' => string '7' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1ea63d49335001d42' (length=24) 'nutritionix_cal' => string '160' (length=3) 3 => array (size=8) 'id' => string '98' (length=2) 'name' => string 'asian sesame' (length=12) 'description' => string '' (length=0) 'price' => string '0.69' (length=4) 'ingredienttype_id' => string '7' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1f9655f6d35001d3d' (length=24) 'nutritionix_cal' => string '125' (length=3) 4 => array (size=8) 'id' => string '114' (length=3) 'name' => string 'arugula ' (length=8) 'description' => string 'local' (length=5) 'price' => string '0.99' (length=4) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1f9655f6d35001d24' (length=24) 'nutritionix_cal' => string '35' (length=2) array (size=3) [greens] 0 => array (size=8) 'id' => string '6' (length=1) 'name' => string 'spinach' (length=7) 'description' => string '' (length=0) 'price' => string '0.69' (length=1) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1f9655f6d35001d40' (length=24) 'nutritionix_cal' => string '55' (length=2) 1 => array (size=8) 'id' => string '8' (length=1) 'name' => string 'kale' (length=4) 'description' => string 'local' (length=5) 'price' => string '0' (length=1) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1ea63d49335001d48' (length=24) 'nutritionix_cal' => string '71' (length=2) 2 => array (size=8) 'id' => string '114' (length=3) 'name' => string 'arugula ' (length=8) 'description' => string 'local' (length=5) 'price' => string '0.99' (length=4) 'ingredienttype_id' => string '1' (length=1) 'active' => string '1' (length=1) 'nutritionix_id' => string '529e7dd1f9655f6d35001d24' (length=24) 'nutritionix_cal' => string '35' (length=2)
from greens, have record arugula's price, since it's highest. check has performed each new addition, or possible removal of arugula.
i'm thinking i'm going using multiple foreach loops, after getting $prices
array
- get
$greens
array - get max prices , record item id
- re-iterate on original
$prices
array , set prices of ingredients, matchingredienttype_id == 1
, 0 while leaving output of max.
so i'm sort of stuck 3 foreach
loops desired outcome. there better way or way go?
i suggest track user's selected ingredients on database, allow query total price.
when user adds/removes ingredient, can update join table consisting of following columns:
selected_ingredients (user_id, ingredient_id)
then can calculate maximum price per ingredient type:
select max(price), ingredienttype_id ingredients join selected_ingredients si on si.ingredient_id = i.id si.user_id = ? group i.ingredienttype_id
or total as
select sum(price) ( select max(price) price, ingredienttype_id ingredients join selected_ingredients si on si.ingredient_id = i.id si.user_id = ? group i.ingredienttype_id ) t1
if you're tracking selected products in session, don't need record selections in join table , use following query, instead:
select sum(price) ( select max(price) price, ingredienttype_id ingredients id in (?) group ingredienttype_id ) t1
Comments
Post a Comment