title: Matching IDs in 2 different foreach loops in PHP (CodeIgniter)
body: <p>So basically, what I'm trying to achieve is calculating the tax percentage on each food category.</p>
  <p>Lets suppose <code>category number 5</code> having the <code>tax percentage of 8%</code>.</p>
  <p>So based on simple <strong>formula</strong> to calculate <strong>tax</strong> on each item is:</p>
  <blockquote>
  <p><strong>ITEM COST</strong> * <strong>TAX %</strong> / <strong>100</strong> = <strong>Tax Amount</strong></p>
  </blockquote>
  <p><strong>So I have 2 arrays</strong></p>
  <p><strong>ARRAY 1</strong>:</p>
  <pre><code> Array
  (
      [0] =&gt; Array
          (
              [5] =&gt; 8    // [CATEGORY-ID] =&gt; TAX %
          )
  
      [1] =&gt; Array
          (
              [22] =&gt; 9
          )
  
      [2] =&gt; Array
          (
              [32] =&gt; 0
          )
  
  )
  </code></pre>
  <p><strong>ARRAY 2</strong>:</p>
  <pre><code> Array
  (
      [0] =&gt; Array
          (
              [5] =&gt; 4.6   // [CATEGORY-ID] =&gt; ITEM COST
          )
  
      [1] =&gt; Array
          (
              [5] =&gt; 10.33
          )
  
      [2] =&gt; Array
          (
              [22] =&gt; 1.15
          )
  
      [3] =&gt; Array
          (
              [32] =&gt; 4.59
          )
  
  )
  </code></pre>
  <p><strong>table food-category</strong>
  <a href="https://i.stack.imgur.com/SZA7Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SZA7Y.png" alt="here" /></a></p>
  <p>Some sub functions used inside <strong>get_per_tax_amount()</strong></p>
  <pre><code>/**
    * GET CATEGORIES-ID FROM ADDED CART ITEM OF THE USER
   */
  public function get_category_id_from_cart_item()
      {
           $cart_items = $this-&gt;cart_model-&gt;get_all();
  
           $data = array();
           foreach ($cart_items as $cart_item) {
               
              $menu_details = $this-&gt;menu_model-&gt;get_cat_by_menu_id($cart_item['menu_id']);
              $data[] = $menu_details['category_id']; //array(1, 2, 2, 3);
           }
  
           return  array_unique($data); // Array is now unique (1, 2, 3)
  
      }
  
      /**
       * GET TAX % FROM CATEGORY-ID
       */
      public function get_category_tax_value()
      {
           
           $cat_ids = $this-&gt;get_category_id_from_cart_item();
           $data = array();
           foreach ($cat_ids as $cat_id) {
              $this-&gt;db-&gt;select('id,tax');
              $this-&gt;db-&gt;where('id', $cat_id);
              $category_data = $this-&gt;db-&gt;get($this-&gt;table)-&gt;row_array();
              $data[] =  array($category_data['id'] =&gt; $category_data['tax']); 
            }
         return $data;
      }
  
      /**
       * GET SMALLER DATA FOR CART PAGE : PER-MENU PRICE
       */
      public function get_per_menu_price()
      {
          $cart_details = $this-&gt;get_all();
          $data = array(); 
          foreach ($cart_details as $cart_detail) {
    
              $menu_details = $this-&gt;menu_model-&gt;get_cat_by_menu_id($cart_detail['menu_id']);
              $data[] = array($menu_details['category_id'] =&gt; $cart_detail['price']);               
              
          }
          return $data;
          
      }
  </code></pre>
  <p>Example:</p>
  <pre><code>public function get_per_tax_amount()
      {
           $percentages = $this-&gt;category_model-&gt;get_category_tax_value();  //array 1
           $units = $this-&gt;cart_model-&gt;get_per_menu_price(); //array 2
           $total_vat = array();
           foreach ($percentages as $cat =&gt; $proportion) {
              
              foreach ($units as $unit_cat =&gt; $price) {
                  
                  if($cat == $unit_cat){
  
                       $total_vat[] = (floatval($price) * floatval($proportion)) / 100;
  
                  }else{
  
                       $total_vat[] = (floatval($price) * floatval($proportion)) / 100;
                  }
              }
           }
  
           return $total_vat;
      }
  </code></pre>
  <p><strong>output is:</strong></p>
  <pre><code>Array
  (
      [0] =&gt; 0.01
      [1] =&gt; 0.01
      [2] =&gt; 0.01
      [3] =&gt; 0.01
      [4] =&gt; 0.01
      [5] =&gt; 0.01
      [6] =&gt; 0.01
      [7] =&gt; 0.01
      [8] =&gt; 0.01
      [9] =&gt; 0.01
      [10] =&gt; 0.01
      [11] =&gt; 0.01
  )
  </code></pre>
  <p>I don't know why I am getting this output.</p>
  <p>So my problem is, how do I match the <strong>CATEGORY-ID</strong> using foreach loop and get each item tax amount?</p>