PHP Arrays

|  | Search W3Schools : |

HOME | HTML | CSS | XML | JAVASCRIPT | ASP | PHP | SQL | MORE... |  | References | Examples | Forum | About

ADVERTISEMENTS XML Certification Download XML editor Custom Programming |  |

Table of contents

PHP Basic
PHP HOME
PHP Intro
PHP Install
PHP Syntax
PHP Variables
PHP String
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP Looping
PHP Functions
PHP Forms
PHP $_GET
PHP $_POST
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
PHP Database
MySQL Introduction
MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP and AJAX
AJAX Introduction
XMLHttpRequest
AJAX Suggest
AJAX XML
AJAX Database
AJAX responseXML
AJAX Live Search
AJAX RSS Reader
AJAX Poll
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
PHP Quiz
PHP Quiz
PHP Exam

Selected Reading

Web Statistics
Web Glossary
Web Hosting
Web Quality
W3Schools Tutorials
W3Schools Forum
Helping W3Schools

# PHP Arrays

An array can store one or more values in a single variable name.

## What is an array?

When working with PHP, sooner or later, you might want to create many similar variables.

Instead of having many similar variables, you can store the data as elements in an array.

Each element in the array has its own ID so that it can be easily accessed.

There are three different kind of arrays:

- Numeric array - An array with a numeric ID key
- Associative array - An array where each ID key is associated with a value
- Multidimensional array - An array containing one or more arrays

## Numeric Arrays

A numeric array stores each element with a numeric ID key.

There are different ways to create a numeric array.

### Example 1

In this example the ID key is automatically assigned:

    $names = array("Peter","Quagmire","Joe");

### Example 2

In this example we assign the ID key manually:

    $names[0] = "Peter";
    $names[1] = "Quagmire";
    $names[2] = "Joe";

The ID keys can be used in a script:

    <?php

    $names[0] = "Peter";
    $names[1] = "Quagmire";
    $names[2] = "Joe";

    echo $names[1] . " and " . $names[2] .
    " are ". $names[0] . "'s neighbors";
    ?>

The code above will output:

    Quagmire and Joe are Peter's neighbors

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

    $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

### Example 2

This example is the same as example 1, but shows a different way of creating the array:

    $ages['Peter'] = "32";
    $ages['Quagmire'] = "30";
    $ages['Joe'] = "34";

The ID keys can be used in a script:

    <?php

    $ages['Peter'] = "32";
    $ages['Quagmire'] = "30";
    $ages['Joe'] = "34";

    echo "Peter is " . $ages['Peter'] . " years old.";
    ?>

The code above will output:

    Peter is 32 years old.

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

    $families = array
    (
      "Griffin"=>array
      (
      "Peter",
      "Lois",
      "Megan"
      ),
      "Quagmire"=>array
      (
      "Glenn"
      ),
      "Brown"=>array
      (
      "Cleveland",
      "Loretta",
      "Junior"
      )
    );

The array above would look like this if written to the output:

    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:

    echo "Is " . $families['Griffin'][2] .
    " a part of the Griffin family?";

The code above will output:

    Is Megan a part of the Griffin family?

## Learn XML with <oXygen/> XML Editor - Free Trial!

oXygen helps you learn to define, edit, validate and transform XML documents. Supported technologies include XML Schema, DTD, Relax NG, XSLT, XPath, XQuery, CSS.

Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger!

Do you have any XML related questions? Get free answers from the oXygen XML forum and from the video demonstrations.

Download a FREE 30-day trial today!

WEB HOSTING
Ecommerce Components for ASP.NET
$15 Domain Name Registration Save $20 / year!
Buy UK Domain Names Register Domain Names
Cheap Domain Names Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting

WEB BUILDING
Website Templates Flash Templates Website Builder Internet Business Opportunity
Custom Programming
FREE Trial or Demo Web Content Manager Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates

EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification

HOME or Top of Page |  | Printer Friendly
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2009 by Refsnes Data. All Rights Reserved.