help with two dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

help with two dimensional array

Post by gammaman »

How would I take the following array and add up and average each student, then print them out after using asort(). (This is NOT a HW assignment, I am just having a lot of trouble understanding 2d arrays and how they work so I thought I would set up an example.

$Student = array ( array (92,100,91),
array(96,82,77),
array(60,80,70),
array(90,90,90) );
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: help with two dimensional array

Post by Kieran Huggins »

foreach to loop through the outer array, then deal with the inner arrays:

Code: Select all

foreach($students as $student){
   echo array_sum($student)/count($student);
}
Post Reply