PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP arrays

Arrays are a very important concept in PHP as well as many other programming languages. An array is a special type of variable which can hold a list of information.

This lesson focuses on:

The necessity of arrays

Imagine you are writing a program that has three variables which contain the names of cars, your code might look like this:

<?php
$car1 = "Toyota Camry";
$car2 = "Honda Accord";
$car3 = "Mitsubishi Galant";
?>

With Arrays, it is much simpler. An array gives you the ability to group together related variables into one set. Arrays are special variables which hold lists of information. Therefore, instead of the code above you can declare a 'cars' array:

<?php
$cars[0] = "Toyota Camry";
$cars[1] = "Honda Accord";
$cars[2] = "Mitsubishi Galant";
?>

Numeric arrays

The numeric array is an array which associates an index number to a certain value.

Creating a numeric array

A numeric array is created by initializing a variable which will be the name of the array together with the array function

Syntax for creating a numeric array:

$arrayName = array();

Example:

<?php
$animals = array();
?>

In the above example, an array named "animals" is created.

Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.

Example:

<?php
$animals = array(4);
?>

In the above example, an array named "animals" that can store 4 elements is created.

Adding values to a numeric array

Once you create a numeric array, you can add values to it by specifying the array name, an index in the array and a value to be placed in that index.

Syntax:

$arrayName[index] = value;

Example:

<?php
$animals[0] = "turtle";
$animals[1] = "frog";
$animals[2] = "rabbit";
$animals[3] = "giraffe";
?>

In the above example, an array named "animals" is declared. Four values are added to it.

NOTE: The first index of an array is 0, not 1!

Another way to assign values to an array involves doing so during the array's initialization.

Example:

$animals = array("turtle", " frog", "rabbit", "giraffe");

Accessing a numeric arrays elements

To access a numeric arrays elements, refer to the array with the appropriate index number in brackets.

NOTE: Array indexes start at 0. So the 1st element of an array would be at index 0, the 5th element of an array would be at index at 4, and so on.

Example:

<?php
$animals[0] = "turtle";
$animals[1] = "frog";
$animals[2] = "rabbit";
$animals[3] = "giraffe";

//print the last element of the animals array
print "The last element in the animals array is " 
. $animals[3];
?>

Output:

The last element in the animals array is giraffe

Modifying a numeric arrays elements

To modify a numeric arrays elements, refer to the array with the appropriate index number in brackets of the value you want to change and set it to the new value.

Example:

<?php
$animals[0] = "turtle";
$animals[1] = "frog";
$animals[2] = "rabbit";
$animals[3] = "giraffe";

print "The old value of animals[1]: " . $animals[1];
print "<br />The old value of animals[3]: " . $animals[3];

//change the value of animals[1]
$animals[1] = "porcupine";

//change the value of animals[3]
$animals[3] = "pony";

print "<br />The new value of animals[1]: " . $animals[1];
print "<br />The new value of animals[3]: " . $animals[3];
?>

Output:

The old value of animals[1]: frog
The old value of animals[3]: giraffe
The new value of animals[1]: porcupine
The new value of animals[3]: pony

Associative arrays

The associative array is an array which associates an index value (instead of a number) to a certain value.

Creating an associative array

An associative array is created by initializing a variable which will be the name of the array together with the array function.

Syntax for creating an associative array:

$arrayName = array();

Example:

<?php
$car = array();
?>

In the above example, an array named "car" is created.

Creating an array this way, you can add as many values as you want into it. Alternatively, you can specify an array to be of a particular size.

Example:

<?php
$car = array(4);
?>

In the above example, an array named "cars" that can store 4 elements is created.

Adding values to an associative array

Once you create an associative array, you can add values to it by specifying the array name, an index value in the array and a value to be placed in that index.

Syntax:

$arrayName["index_value"] = value;

Example:

<?php
$car["company"] = "Mercub";
$car["model"] = "Grand Vehicle";
$car["year"] = 1989;
$car["mileage"] = 150,000;
?>

In the above example, an array named "cars" is declared. Four values are added to it.

Another way to assign values to an associative array involves doing so during the array's initialization.

Example:

<?php
$car = array("company"=>"Mercub", "model"=>"Grand Vehicle", 
"year"=>"1989", "mileage"=>"150,000");
?>

Accessing an associative arrays elements

To access an associative arrays elements, refer to the array with the appropriate index value in brackets.

Example:

<?php
$car["company"] = "Mercub";
$car["model"] = "Grand Vehicle";
$car["year"] = 1989;
$car["mileage"] = 150000;

//print the first element of the cars array
print "The first element in the cars array is " 
. $car["company"];
?>

Output:

The first element in the cars array is Mercub

Modifying an associative arrays elements

To modify an associative arrays elements, refer to the array with the appropriate index value in brackets of the value you want to change and set it to the new value.

Example:

<?php
$car["company"] = "Mercub";

$car["model"] = "Grand Vehicle";

$car["year"] = 1989;

$car["mileage"] = 150000;

print "Car company: " . $car["company"];
print "<br />Car year: " . $car["year"];

//change the value of $car["company"]
$car["company"] = "Bord";

//change the value of $car["year"];
$car["year"] = 1990;

print "<br />Car company: " . $car["company"];
print "<br />Car year: " . $car["year"];
?>

Output:

Car company: Mercub
Car year: 1989
Car company: Bord
Car year: 1990

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments