title: How to combine all rows in array as one input, with foreach()?
body: <p>I am trying to combine all the rows in an array into one row for a product-order summary. So what i have right now </p>
  
  <p><code>$cartitem = $item['name'] ."  qty  ". $item['quantity'] ."  IDR ". $item['price']. "  IDR " . $item_price;</code>
  <code></code></p>
  
  <p><code>foreach($cartitem as $rows)</code></p>
  
  <p><code>{ echo $rows;}</code></p>
  
  <p>Output (as of now) = Product Name qty 2 IDR 15000 IDR 30000</p>
  
  <p>This is the output i am aiming for, but i need to display all rows. The output above only returns the last row. Is this possible with <code>foreach()</code>, then to <code>implode</code> all rows to insert into a single column in database?</p>
  
  <p>*UPDATE:
  So this is the source of the input arrays(cart system)
  <em>skip a few lines</em></p>
  
  <pre><code>$item_array = array($productByCode["code"]=&gt;array('name'=&gt;$productByCode["name"], 'code'=&gt;$productByCode["code"], 'quantity'=&gt;$_POST["quantity"], 'price'=&gt;$productByCode["price"], 'image'=&gt;$productByCode["image"]));
  
              if(!empty($_SESSION["cart_item"])){
                  if(in_array($productByCode["name"], array_keys($_SESSION["cart_item"]))){
                  foreach($_SESSION["cart_item"] as $k =&gt; $v){
                      if($productByCode["code"] == $k){
                          if(empty($_SESSION["cart_item"][$k]["quantity"])){
                          $_SESSION["cart_item"][$k]["quantity"] = 0;
                          }
                          $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                          }
                      }
                  }  else { 
                      $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$item_array);
                  }
                  } else {
                      $_SESSION["cart_item"] = $item_array;
  // Above is the cart add-product action
  </code></pre>
  
  <p>Here is the cart ouput session </p>
  
  <pre><code>foreach($_SESSION["cart_item"] as $item){
      $item_price = $item["quantity"]*$item["price"];
  ?&gt;
      &lt;tr&gt;
      &lt;td&gt;&lt;img src="&lt;?php echo $item['image'];?&gt;" class="cart-item-image"&gt;&lt;?php echo $item["name"]; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $item['name']; ?&gt;&lt;/td&gt;
      &lt;td style="text-align:right;"&gt;&lt;?php echo $item['quantity']; ?&gt;&lt;/td&gt;
      &lt;td style="text-align:right;"&gt;&lt;?php echo "IDR ".$item['price']; ?&gt;&lt;/td&gt;
      &lt;td style="text-align:right;"&gt;&lt;?php echo "IDR ". number_format($item_price,2); ?&gt;&lt;/td&gt;
      &lt;td style="text-align-center;"&gt;&lt;a href="cart2.php?action=remove&amp;code=&lt;?php echo $item['code'];?&gt;" class="btn-remove"&gt;Hapus&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;?php
      $total_quantity += $item["quantity"];
      $total_price += ($item_price);
      // Count rows input
      echo count($_SESSION["cart_item"]);
  }
  ?&gt;
  // Cart Output Session
  
  </code></pre>
  
  <p>I have tried to check the rows by <code>print_r($_SESSION["cart_item"])</code>, and it shows in formation as in <code>$item_array</code>. I need a neater output to store into one column in database.</p>