Accessing Arrays
So far, you’ve seen the ways in which you can create and add to arrays. In this section, you will examine some of the tools PHP provides to allow you to acquire information about arrays and access their elements.
Getting the Size of an Array
You can access an element of an array by using its index:
print $user[4]
Because of the flexibility of arrays, however, you won’t always know how many elements a particular array contains. That’s where the count() function comes into play. count() returns the number of elements in an array. In the following code, we define a numerically indexed array and use count() to access its last element:
$users = array ("Bert", "Sharon", "Betty", "Harry" ); print $users[count($users)-1];
Notice ...
Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.