Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    php
  » Arrays for beginners
      by Mike Milano
 Page 4 of 5 
< Previous     Next >

(Login to remove green text ads)
Multi-Dimensional Array
Multi-Dimensional Array

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 :)




 
 Page 4 of 5 
< Previous     Next >

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting