EllisLab text mark
Advanced Search
     
multidimensional array question..
Posted: 08 March 2012 12:35 PM   [ Ignore ]
Avatar
Joined: 2012-01-02
117 posts

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

 Signature 

Perfection can be quantified by measurement, measure yourself…

 
Posted: 08 March 2012 01:38 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-03-31
205 posts
$sum 0;
foreach(
$array as $arr{
 $count 
$arr['count'];
 
$sum += $count;
 Signature 

Tell me and I forget. Teach me and I remember. Involve me and I learn. ~ Benjamin Franklin

 
Posted: 09 March 2012 06:56 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-01-02
117 posts

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

array['counts'

, this is a tricky one anyone else any advice and thankyou for your responce.

 Signature 

Perfection can be quantified by measurement, measure yourself…

 
Posted: 09 March 2012 06:59 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-01-02
117 posts

My mistake echo’d the wrong variable, thanks a bunch it does work, great stuff much appreciated…

 Signature 

Perfection can be quantified by measurement, measure yourself…