Array Notation (Literals)
PHP provides array() and [] as notations (literals) for creating (declaring) or representing arrays. Using array() or [], you can easily create indexed arrays, associative arrays, and multidimensional arrays, or initialize arrays.
- Literal
- A notation for creating values using characters or symbols that humans can understand.
Before PHP 5.4, the only array notation available was array().
Since PHP 5.4, you can use the short array syntax that replaces array() with [].
Syntax
// array() notation
array(value1, value2, value3, ...)
// [] notation
[index => value, index => value, index => value, ...]
// array() notation
array(index => value, index => value, index => value, ...)
// [] notation
[index => value, index => value, index => value, ...]
A comma (,) after the last element of an array is valid syntax. This is the same for both the array() notation and the [] notation.
// array() notation
array(
1,
2,
3, // A comma (,) after the last element is valid syntax.
);
// [] notation
[
1,
2,
3, // A comma (,) after the last element is valid syntax.
];
array() Parameters
value |
Defines the values of the array. |
|---|---|
index |
Defines the index for each value. This index can be a string or an integer.
|
array() Return Values
Returns an array of the parameters.
Examples
Empty Array
This is a way to represent an empty array with no elements.
// array() notation
$empty_array = array();
// [] notation
$empty_array = [];
print_r($empty_array);
// Output: Array ( )
Indexed Array
An indexed array is an array where each element is assigned a sequential numeric index.
In PHP, indexed arrays allow you to access array elements using numeric indexes. These arrays have integer indexes starting from 0, and each index represents the value at that position.
// array() notation
$fruits = array('Apple', 'Banana', 'Cherry');
// [] notation
$fruits = ['Apple', 'Banana', 'Cherry'];
echo $fruits[0]; // Output: 'Apple'
echo $fruits[1]; // Output: 'Banana'
echo $fruits[2]; // Output: 'Cherry'
Associative Array
An associative array is an array that allows each element to be accessed using a user-defined key (string or integer). The elements of an associative array consist of key and value pairs in the key => value format. These key and value pairs are used to identify the elements of the array.
// array() notation
$ages = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
// [] notation
$ages = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
echo $ages["Peter"]; // Output: 35
echo $ages["Ben"]; // Output: 37
echo $ages["Joe"]; // Output: 43
Multidimensional Array
A multidimensional array is an array that contains other arrays within its elements. It is useful for representing complex data in an intuitive and efficient structure.
// array() notation
$cars = array(
array('Volvo', 22, 18),
array('BMW', 15, 13),
array('Saab', 5, 2),
array('Land Rover', 17, 15)
);
// [] notation
$cars = [
['Volvo', 22, 18],
['BMW', 15, 13],
['Saab', 5, 2],
['Land Rover', 17, 15]
];
echo $cars[0][0]; // Output: 'Volvo'
echo $cars[1][0]; // Output: 'BMW'
echo $cars[2][0]; // Output: 'Saab'
echo $cars[3][0]; // Output: 'Land Rover'
Mixed Array
An array that contains elements with integer keys and elements with string keys is called a "Mixed Array."
// array() notation
$mixed_array = array(
'name' => 'John',
'age' => 30,
'hobbies' => array('Soccer', 'Reading'),
'address' => '123 Main Street'
);
// [] notation
$mixed_array = [
'name' => 'John',
'age' => 30,
'hobbies' => ['Soccer', 'Reading'],
'address' => '123 Main Street'
];
echo $mixed_array['name']; // Output: 'John'
echo $mixed_array['age']; // Output: 30
echo $mixed_array['hobbies'][0]; // Output: 'Soccer'
echo $mixed_array['hobbies'][1]; // Output: 'Reading'
echo $mixed_array['address']; // Output: '123 Main Street'