Arrays: What Is An Array?
Arrays: What Is An Array?
Page 1 of 4
TRANSLATE
Search w3schools.com
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...
Search
YOUR FREE WEBSITE Free Flash Website Free Website Builder Free Web Design
PHP Basic
PHP HOME PHP Intro PHP Install PHP Syntax
PHP Arrays
Previous
An array stores multiple values in one single variable.
Next Chapter
PHP MySQL Hosting Best Hosting Coupons UK Reseller Hosting Cloud Hosting Top Web Hosting $3.98 Unlimited Hosting Premium Website Design WEB BUILDING XML Editor - Free Trial! FREE Website BUILDER Free Website TemplatesFree CSS Templates Make Your Own Website
PHP Variables PHP String PHP Operators PHP If...Else PHP Switch PHP Arrays PHP While Loops PHP For Loops PHP Functions PHP Forms PHP $_GET PHP $_POST
What is an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
PHP Advanced
PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter
W3SCHOOLS EXAMS Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP W3SCHOOLS BOOKS New Books: HTML, CSS JavaScript, and Ajax STATISTICS Browser Statistics Browser OS Browser Display SHARE THIS PAGE Share with
PHP Database
MySQL Introduction MySQL Connect MySQL Create MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Update MySQL Delete PHP ODBC
Numeric Arrays
A numeric array stores each array element with a numeric index. There are two methods to create a numeric array. 1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually:
PHP XML
XML Expat Parser XML DOM XML SimpleXML
Example
In the following example you access the variable values by referring to the array name and index:
http://www.w3schools.com/php/php_arrays.asp
3/1/2012
PHP Arrays
Page 2 of 4
AJAX Poll
$cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars."; ?>
The code above will output:
PHP Reference
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQL PHP SimpleXML PHP String PHP XML PHP Zip
Associative Arrays
An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.
Example 1
In this example we use an array to assign ages to the different persons:
PHP Quiz
PHP Quiz PHP Certificate
Example 2
This example is the same as example 1, but shows a different way of creating the array:
< ?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?>
The code above will output:
Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
Example
In this example we create a multidimensional array, with automatically assigned ID keys:
http://www.w3schools.com/php/php_arrays.asp
3/1/2012
PHP Arrays
Page 3 of 4
Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) )
Example 2
Lets try displaying a single value from the array above:
Previous
Next Chapter
http://www.w3schools.com/php/php_arrays.asp
3/1/2012
PHP Arrays
Page 4 of 4
www.SoftwareShelf.com
TOP | PRINT |
FORUM |
ABOUT
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2012 by Refsnes Data. All Rights Reserved.
http://www.w3schools.com/php/php_arrays.asp
3/1/2012