On the last page we ended off with a function that returns an array of people. The only thing different than the function that returned an array of fruit is that the items inside the people array are arrays themselves.
In this example I will manually create this multi-dimensional array. This code is for the purpose of communicating how the array works. In the real-world, you will most likely be using a method similar to the last database example.
PHP Code:
// initialize people array
$people = array();
// create a person array
$person = array();
$person['first_name'] = 'John';
$person['last_name'] = 'Smith';
$pseron['username'] = 'jsmith';
// append people array
$people[] = $person;
// create another person
$person = array();
$person['first_name'] = 'Martha';
$person['last_name'] = 'Stewart';
$pseron['username'] = 'mstewart';
// append people array
$people[] = $person;
At this point you can see we have 2 items in our people array. Here I will manually print out each username so you can see the format to access the embedded array.
PHP Code:
// echo the first person's username
// we will use the key 0 since this is our first person
echo $people[0]['username'];
echo "<br>";
// key 1 is the next person
echo $people[1]['username'];
$people[0] is the same as $person. So $people[0]['username'] is the same as $person['username']. I can only hope I am making sense :)