Welcome Guest, Not a member yet? Register   Sign In
multidimensional array question..
#1

[eluser]ppwalks[/eluser]
I am still learning PHP and I think it is easier to learn with something like cl, but I would love it someone would just give me some PHP help, this is not a CL problem.

I have a multidimensional array like the following:

Array ( [1] => Array ( [name] => first item [count] => 3 ) [5] => Array ( [name] => second item [count] => 1 )),

Now from some reading I gather you would use a for loop for the problem but I have not been able to find out for definate so if someone would be kind to show then it would be most appreciated.

The array is generated from a session variable and I would like to extract the "count" and then add all the "counts together", is their a simple way to achieve this.

I'll keep googling until someone responds...

Thanks Guys
#2

[eluser]Matalina[/eluser]
Code:
$sum = 0;
foreach($array as $arr) {
$count = $arr['count'];
$sum += $count;
}
#3

[eluser]ppwalks[/eluser]
Still having the same problem even with your code, it is similar to what I'd already written it is simple returning the count value of the last array within the array not adding the total value of all the
Code:
array['counts']
, this is a tricky one anyone else any advice and thankyou for your responce.
#4

[eluser]ppwalks[/eluser]
My mistake echo'd the wrong variable, thanks a bunch it does work, great stuff much appreciated...




Theme © iAndrew 2016 - Forum software by © MyBB