Arrays are variables used to store multiple values in a single variable name, each value being a string or anumber or even another array.
Arrays are structured as a series of key-value pairs, where one pair is an element of that array. For each item in the list, there is a key (or index) associated with it.
In PHP there are two kind of Arrays: Numeric Arrays (known as indexed arrays) and Associative Arrays.

Numeric Array

A numeric array (or indexed array) stores each array element with a numeric index /key (generally, consecutive, and starting at 0).
There are two primary methods to create a numeric array.

1) Creating Array with the square bracket syntax, adding one element at a time.
  - Syntax:
$array[0] = value0;
$array[1] = value1;
$array[2] = value2;
 ...
The key (within square bracket) can start at any number.
- You can use this method in the fallowing way too (without adding the keys manually):
$array[] = value0;
$array[] = value1;
$array[] = value2;
 ...
In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
  - Example:
<?php
// specifying the keys for each element
$array[0] = 'php';
$array[1] = 'mysql';
$array[2] = 8;

// without specifying the keys
$fruits[] = 'apple';
$fruits[] = 'banana';
$fruits[] = 'apricots';
?>

2) Creating Array with the array() function, adding the key=>value pairs in one step, and separated by commas.
  - Syntax:
$array = array(0=>value0, 1=>value1, 2=>value2);
The first key can start at any number. You can add as many key=>value pairs as you want (separated by commas).
- You can use this method in the fallowing way too (without adding the keys manually):
$array = array(value0, value1, value2);
In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
  - Example:
<?php
// specifying the keys for each element
$array = array(0=>'php', 1=>'mysql', 2=>8);

// without specifying the keys
$fruits = array('apple', 'banana', 'apricots');
?>

The keys of any array have to be named uniquely, otherwise each identical key assigned will replace the one before it.
If you want to create an empty array, just use the array function without any parameters:
                    $arr = array();

Accessing a specific Array's element

To access the value of a specific element of an array start with the array variable name, followed by the key in square brackets:
$var_name = $array[key];
The value of the array element with the specified key will be stored in the $var_name variable.
  - Example:
<?php
$array = array('php', 'mysql', 8);

$elm = $array[1];
echo $elm. '<br />';           // mysql

echo $array[0]. ' course';               // php course
?>
In this example the keys aren't added when the array is defined, but we know that PHP will assign automatically the keys, starting at 0.
The code above will output:
mysql
php course

To get the number of elements in an array you can use the count() function:
                    $nr = count($array);

Modify and Add values in an Array

To modify a value in an array, assign a new value to the element with a specified index number.
  - Syntax:
array_name[index] = 'new value';
If the array_name[index] element isn't defined in that array, will be added automatically with the 'new value'. This method can be used when you want to add new elements in an array.
  - Example:
<?php
// define an array
$fruits = array('apple', 'banana', 'apricots');

// change the value of first element
$fruits[0] = 'figs';

$nr = count($fruits);              // gets the number of items in $fruits
echo 'There is '. $nr. ' elements in the array. The first is: '. $fruits[0];

// adds a new item in $fruits
$fruits[] = 'nuts';

$nr = count($fruits);              // gets again the number of items in $fruits
echo '<br /> Now, the $fruits array has '. $nr. ' items';
?>
To add a new element in a numeric array, you can also use the syntax:   $array[] = value; (for example:   $fruits[] = 'nuts';).
This code will output:
There is 3 elements in the array. The first is: figs
Now, the $fruits array has 4 items

Traversing numeric Arrays

To access every element in a numeric array, use the for() loop instruction.
  - Syntax:
$nr = count($array);        // gets the number of elements
for($i=0; $i<$nr; $i++) {
  // PHP instructions for the curent element:  $array[$i];
}
  - Example:
<?php
$arr = array('php', 'mysql', 8);

$nr = count($arr);
for($i=0; $i<$nr; $i++) {
  $elm = $arr[$i];
  echo '<br /> Item '. $i. ' has the value: '. $elm;
}
?>
Output;
Item 0 has the value: php
Item 1 has the value: mysql
Item 2 has the value: 8

Associative Arrays

Associative arrays use strings in place of index numbers, to associate the value of the elements.
There are two methods to create an associative array.

1) Creating Array with the square bracket syntax, adding one element at a time.
  - Syntax:
$array['key1'] = value1;
$array['key2'] = value2;
$array['key3'] = value3;
 ...
You can use either single or double quotes.
  - Example:
<?php
$sites['en'] = 'www.courseweb.net';
$sites['ro'] = 'www.marplo.net';
$sites['search'] = 'www.google.com';
?>

2) Creating Array with the array() function, adding the 'key'=>value pairs in one step, and separated by commas.
  - Syntax:
$sites = array('key1'=>value1, 'key2'=>value2, 'key3'=>'value3');
You can add as many 'key'=>value pairs as you want (separated by commas).
  - Example:
<?php
$sites = array('en'=>'www.courseweb.net', 'ro'=>'www.marplo.net', 'search'=>'www.google.com');
?>

You can refer to a specific element of an associative array, to access or change its value, the same as you would an indexed array, using the key string instead of a number.
  - Example:
<?php
// define the array
$sites['ro'] = 'www.courseweb.net';
$sites['en'] = 'www.marplo.net';
$sites['search'] = 'www.google.com';

echo 'Search: '. $sites['search'];             // Search: www.google.com

// change the value of 'search' item
$sites['search'] = 'www.yahoo.com';

echo '<br /> Search: '. $sites['search'];      // Search: www.yahoo.com
?>
This code will output:
Search: www.google.com
Search: www.yahoo.com

Traversing associative Arrays

You can walk through, or traverse, an associative array with the foreach() loop.
  - Syntax:
foreach ($array as $key => $val) {
  // Do something with $key and /or $val.
}
This construct will place the key and value of the current $array element into their own variables: $key and $val.
It will allow you to step through each element of an array and implement code on its key and value.
  - Example:
<?php
// define the array
$sites['ro'] = 'www.courseweb.net';
$sites['en'] = 'www.marplo.net';
$sites['search'] = 'www.google.com';

// traversing $sites
foreach ($sites as $key => $val) {
  echo '<br /> The key is: '. $key. ' and its value is: '. $val;
}
?>
This code will output:
The key is: ro and its value is: www.courseweb.net
The key is: en and its value is: www.marplo.net
The key is: search and its value is: www.google.com


• Starting with version PHP 5.4 has been added a new way to define arrays, using short array syntax.
E.g.
$arr = [];              // empty array, instead of array();

$arr = [1, 2, 3, 4];    // numeric array

$arr = ['ro'=>'courseweb.net', 'en'=>'marplo.net', 'num'=>8];       // associative array
- The array created with this short syntax can be accessed, modified and traversed as any array.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
PHP Arrays

Last accessed pages

  1. Qwop (14581)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (34163)
  3. Backgammon (3874)
  4. PHP functions (1299)
  5. Construction Fall (119)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (50849)
  2. Read Excel file data in PHP - PhpExcelReader (39684)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37892)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (34163)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (31636)