title: PHP: Comparing an array, getting largest value and updating
body: <p>I have an array that's submitted from a menu and I have to take the largest value of a subcategory and only use the highest price. The updates can occur multiple times as the user adds more items.</p>
  
  <p><code>User Scenario</code>: User clicks on an ingredient, then another, then another. During each addition, prices are updated dynamically.</p>
  
  <p>One of the subcategories is <code>salad greens</code> or <code>ingredienttype_id = 1</code>. </p>
  
  <p>Current Code:</p>
  
  <pre><code>  $prices = $db-&gt;get_ingredient_prices($list);
    $free_items = $db-&gt;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 &gt; $qtyB){
            $free_items[$price['ingredienttype_id']] = $qtyA - $qtyB;
            $qtyC = 0;
          } elseif($qtyA &lt; $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 &gt; -1) {
          $customizations_total = $customizations_total + ($price['price'] * $qtyC);
        } else {
          $customizations_total = $customizations_total + $price['price'];
        }
      }
  
      return number_format($customizations_total,2);
  </code></pre>
  
  <p>Now I have to create my loop within this sample code, somehow. </p>
  
  <p>Here is a sample output from a foreach loop when attempting to get the proper category:</p>
  
  <pre><code>$list = [4, 6, 8, 114, 98];
  $prices = $db-&gt;get_ingredient_prices($list);
  
    foreach ($prices as $k =&gt; $v) {
      if ($v['ingredienttype_id'] == '1') { $greens[] = $v; }
    }
  </code></pre>
  
  <p>Sample Output:</p>
  
  <pre><code>array (size=6) [prices]
    0 =&gt; 
      array (size=8)
        'id' =&gt; string '6' (length=1)
        'name' =&gt; string 'Spinach' (length=7)
        'description' =&gt; string '' (length=0)
        'price' =&gt; string '0.69' (length=1)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1f9655f6d35001d40' (length=24)
        'nutritionix_cal' =&gt; string '55' (length=2)
    1 =&gt; 
      array (size=8)
        'id' =&gt; string '8' (length=1)
        'name' =&gt; string 'Kale' (length=4)
        'description' =&gt; string 'Local' (length=5)
        'price' =&gt; string '0' (length=1)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1ea63d49335001d48' (length=24)
        'nutritionix_cal' =&gt; string '71' (length=2)
    2 =&gt; 
      array (size=8)
        'id' =&gt; string '4' (length=1)
        'name' =&gt; string 'Bleu Cheese' (length=11)
        'description' =&gt; string '' (length=0)
        'price' =&gt; string '0.69' (length=4)
        'ingredienttype_id' =&gt; string '7' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1ea63d49335001d42' (length=24)
        'nutritionix_cal' =&gt; string '160' (length=3)
    3 =&gt; 
      array (size=8)
        'id' =&gt; string '98' (length=2)
        'name' =&gt; string 'Asian Sesame' (length=12)
        'description' =&gt; string '' (length=0)
        'price' =&gt; string '0.69' (length=4)
        'ingredienttype_id' =&gt; string '7' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1f9655f6d35001d3d' (length=24)
        'nutritionix_cal' =&gt; string '125' (length=3)
    4 =&gt; 
      array (size=8)
        'id' =&gt; string '114' (length=3)
        'name' =&gt; string 'Arugula ' (length=8)
        'description' =&gt; string 'Local' (length=5)
        'price' =&gt; string '0.99' (length=4)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1f9655f6d35001d24' (length=24)
        'nutritionix_cal' =&gt; string '35' (length=2)
  
  array (size=3) [greens]
    0 =&gt; 
      array (size=8)
        'id' =&gt; string '6' (length=1)
        'name' =&gt; string 'Spinach' (length=7)
        'description' =&gt; string '' (length=0)
        'price' =&gt; string '0.69' (length=1)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1f9655f6d35001d40' (length=24)
        'nutritionix_cal' =&gt; string '55' (length=2)
    1 =&gt; 
      array (size=8)
        'id' =&gt; string '8' (length=1)
        'name' =&gt; string 'Kale' (length=4)
        'description' =&gt; string 'Local' (length=5)
        'price' =&gt; string '0' (length=1)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1ea63d49335001d48' (length=24)
        'nutritionix_cal' =&gt; string '71' (length=2)
    2 =&gt; 
      array (size=8)
        'id' =&gt; string '114' (length=3)
        'name' =&gt; string 'Arugula ' (length=8)
        'description' =&gt; string 'Local' (length=5)
        'price' =&gt; string '0.99' (length=4)
        'ingredienttype_id' =&gt; string '1' (length=1)
        'active' =&gt; string '1' (length=1)
        'nutritionix_id' =&gt; string '529e7dd1f9655f6d35001d24' (length=24)
        'nutritionix_cal' =&gt; string '35' (length=2)
  </code></pre>
  
  <p>From the Greens, I have to ONLY record Arugula's price, since it's the highest. This check has to be performed for each new addition, or even possible removal of Arugula.</p>
  
  <p>I'm thinking that I'm going to be using multiple foreach loops, after getting the <code>$prices</code> array to </p>
  
  <ol>
  <li>Get the <code>$greens</code> array</li>
  <li>Get the max from the prices and record the item ID</li>
  <li>Re-iterate over the original <code>$prices</code> array and set prices of ingredients, that match <code>ingredienttype_id == 1</code>, to 0 while leaving the output of max.</li>
  </ol>
  
  <p>So I'm sort of stuck with 3 <code>foreach</code> loops to get my desired outcome. Is there a better way or is this the only way to go?</p>