How do I calculate the total of a shopping cart? I have the following cart implemented by a custom module I wrote.



The columns are stored in 2 associative arrays.

$jcartItemQty = $_POST['jcartItemQty'];
$jcartItemPrice = $_POST['jcartItemPrice'];


This is what those array could contain:


     [jcartItemQty] => Array
            (
                [0] => 3
                [1] => 1
            )

        [jcartItemName] => Array
            (
                [0] => Armas Jarnefelt - LAULAJAN KOTI
                [1] => Jean Sibelius - Kullervo
            )

        [jcartItemPrice] => Array
            (
                [0] => 110
                [1] => 120
            )


I need to combine somehow the arrays into one that looks like $combined = (3 =&gt; 110, 1 =&gt; 120) in order to be able to calculate the total.
array_merge() is not working because will only concatenate those 2 arrays.

foreach ($combined as $key =&gt; $item) {
  $total += $key * $item;
}


I need to display the total in a form item like this:

foreach ($jcartItemPrice as $key =&gt; $value) { 
  $form['jcartItemPrice'][$key] = array(
    '#type' =&gt; 'textfield',
    '#value' =&gt; isset($value) ? $value : '',
  );
}

$form['total'][$key] = array(
  '#type' =&gt; 'textfield',
  '#value' =&gt; isset($value) ? $value : '',
);


Is there a way to do this?
You can use array_merge() to merge two arrays into one.

It looks like you're trying to overengineer things. This is a simple solution:

$combined = array();
for ($i = 0; $i &lt; count($jcartItemQty); $i++) {
  $combined[] = array($jcartItemQty[$i], $jcartItemPrice[$i]);
}