CodeIgniter Forums
multidimensional array question.. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: multidimensional array question.. (/showthread.php?tid=49946)



multidimensional array question.. - El Forum - 03-08-2012

[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


multidimensional array question.. - El Forum - 03-08-2012

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



multidimensional array question.. - El Forum - 03-09-2012

[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.


multidimensional array question.. - El Forum - 03-09-2012

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