Hi all,

Firstly, must say thanks to Sneakyimp for their help in my last thread to helping me come to a solution.

Right, next part of my adventure is totaling a quantity into one number of multiple orders.

For example, if user orders 7 of one item, 3 of another items and 9 of another the total qty for the order is 19 units.

I understand that count() will give me the number of array values within an array and COUNT_RECURSIVE will give me total number of values including array value.

So, if:

$a[] = 7;
$a[] = 3;
$a[] = 9;

$b = count($a); //would output 3?
$c = count($a, COUNT_RECURSIVE); //would output 6?

However, my script retrieves quantitys for items as seperate values within a for loop which I have consolodated into one array, which I've checked with is_array() function. But when I come to add them, I don't get the number I want...

Here is the chunk of code I'm using:

for ($i = 0; $i < $numItem; $i++) {
		extract($cartContent[$i]);
		$pd_name = "$ct_qty x $pd_name";
		$url = "index.php?c=$cat_id&p=$pd_id";

	$subTotal += $pd_price * $ct_qty;

$qtytotal[] = array($ct_qty);

} // end while

foreach ($qtytotal as $totqty){
print_r (count($totqty));
}

I've also tried without a foreach loop, but can't get my head around it.

It seems a simple enough task, but for now I can't quite get my head around it.

Thanks for looking 🙂

    First, you should format your code better before posting (see this sticky thread). Second, you should give your variables more meaningful names:

    $key_count = count($array1);
    $value_sum = array_sum($array1);

    Third, you haven't given enough information about code snippet #2. Is cartContent[] a multi-dimensional array? Are the keys of cartContent[] unique and meaningful? Do they correspond to the appropriate values? Not enough info.

      in response to the above...

      The names of variables are meaningful to me - may not be convention, but its the way that works best for me.

      I've tried array_sum quite a few times in different methods, and none seem to yeild the right result.

      I think cartContent could be multi-dimensional I'm not entirely sure since the whole code is not my own, I'm trying to adapt to what I want the code to achieve. Each value will be unique yes.

      If I echo the variable $ct_qty in the for loop it will give me

      739

      in terms of the numbers in the first post.

      If I add array_sum outside of the for loop it produces (oddly) the number 0. And in a foreach look will give me 739 again.

      Any more suggestions?

        So $ct_qty contains 7, then 3, then 9?

        $qtytotal[] = $ct_qty; 

        In the loop, and array_sum outside it.

        Or just keep a running total. Set $total to 0 before the loop and

        $total += $ct_qty;

        inside.

          Thanks Weedpacket. Guess I just needed to think of simple logic after all!

          The += method worked nicely.
          Thank you.

          Thread resolved.

            Write a Reply...