PHP:比较数组,获得最大值和更新

时间:2016-02-29 04:49:57

标签: php arrays

我有一个从菜单提交的数组,我必须使用子类别的最大值,并且只使用最高价格。当用户添加更多项目时,更新可以多次发生。

User Scenario:用户点击一个成分,然后点击另一个,然后点击另一个。在每次添加期间,价格都会动态更新。

其中一个子类别为salad greensingredienttype_id = 1

当前代码:

  $prices = $db->get_ingredient_prices($list);
  $free_items = $db->get_free_ingredient_types($item);

    foreach($prices as $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);

现在我必须以某种方式在这个示例代码中创建我的循环。

以下是尝试获取正确类别时foreach循环的示例输出:

$list = [4, 6, 8, 114, 98];
$prices = $db->get_ingredient_prices($list);

  foreach ($prices as $k => $v) {
    if ($v['ingredienttype_id'] == '1') { $greens[] = $v; }
  }

示例输出:

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)

来自绿党,我只能记录芝麻菜的价格,因为它是最高的。必须对每次新添加,甚至可能去除芝麻菜进行检查。

我认为在将$prices数组发送到

之后我将使用多个foreach循环
  1. 获取$greens数组
  2. 从价格中获取最大值并记录商品ID
  3. 重新遍历原始$prices数组,并将符合ingredienttype_id == 1的成分价格设置为0,同时保留最大输出
  4. 所以我有点{3}}循环来获得我想要的结果。有更好的方法还是这是唯一的方法?

2 个答案:

答案 0 :(得分:0)

我建议您跟踪用户在数据库中选择的成分,这样您就可以轻松查询总价格。

当用户添加/删除某个成分时,您可以更新由以下列组成的连接表:

selected_ingredients
(user_id, ingredient_id)

然后您可以计算每种成分类型的最高价格:

select max(price), ingredienttype_id
from ingredients i
join selected_ingredients si on si.ingredient_id = i.id
where si.user_id = ?
group by i.ingredienttype_id

或总数为

select sum(price) from (
    select max(price) price, ingredienttype_id
    from ingredients i
    join selected_ingredients si on si.ingredient_id = i.id
    where si.user_id = ?
    group by i.ingredienttype_id
) t1

如果您已经在会话中跟踪所选产品,那么您不需要在连接表中记录选择,而是可以使用以下查询:

select sum(price) from (
    select max(price) price, ingredienttype_id
    from ingredients     
    where id in (?)
    group by ingredienttype_id
) t1

答案 1 :(得分:0)

我在干净的PHP中做了另一个解决方案,以防有人帮助。

步骤1:提交后,我确保项目的数量大于1.提交ID的空值不会起作用。

然后:

// Get all the greens with built in PHP function - very handy.
      $greens = array_filter($prices, function($v) { return $v['ingredienttype_id'] === '1'; });

      // Get the most expensive green and save it
      if ( count($greens) > 1 ) {

        $max_greens = array_reduce($greens, function ($a, $b) {
          return @$a['price'] > $b['price'] ? $a : $b;
        });

        foreach ($prices as $k => $v) {
          if ($v['ingredienttype_id'] === '1' && $v['id'] !== $max_greens['id']) {
            $prices[$k]['price'] = '0';
          }
        }
      }

有点痛,但效果很好。