Web Programming with PHP

13 Arrays

Creation of an array

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments. Array elements can be accessed using the array[key] syntax.

An array can be created using different syntaxes:


<?php
$array = array("foo", "bar", "hallo", "world");
var_dump($array);
?>


<?php
$array = array(0 => "foo", 1 => "bar", 2 => "hallo", 3 => "world");
var_dump($array);
?>


<?php
$array[0] = "foo";
$array[1] = "bar";
$array[2] = "hallo";
$array[3] = "world";
var_dump($array);
?>


<?php
$array[] = "foo";
$array[] = "bar";
$array[] = "hallo";
$array[] = "world";
var_dump($array);
?>

Useful functions

There are quite a few useful functions for working with arrays: