Forum Moderators: coopster

Message Too Old, No Replies

counting total in an array

         

ahmed24

6:08 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



can anyone please tell me how i can count the total in an array?

my array is called $courses and it produces output like this:

Array
(

[1] => Array
(
[FIRST_NAME] => Bob
[LAST_NAME] => Smith
)

[2] => Array
(
[FIRST_NAME] => John
[LAST_NAME] => Townsend
)

)

in the example above there are a total of 2 results but this changes and i want to know how i can determine the total?

thanks

d40sithui

6:40 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



There are two methods that will fit your example.

1.) Use the array length as the "total" since your array is already set up consistently this way.


<?
$total = count($courses);
//OR
$total = sizeof($courses);
?>

2.) The second method is to loop through the whole array.


<?
$total = 0;
foreach(array_keys($courses) as $key){
$total+=1;
}
?>