Initialize an array in PHP
This post will discuss how to create and initialize an array in PHP.
1. Using array() construct
A simple approach to initialize an array in PHP is using the array() construct. It takes "key => values" pairs, where key may be of type string or integer.
The following example creates an associative array using the array() construct.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = array('one' => 1, 'two' => 2, 'three' => 3); print_r($arr); /* Output: Array ( [one] => 1 [two] => 2 [three] => 3 ) */ ?> |
When two identical indexes are defined, the last overwrite the first, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $arr = array("a" => "one", "b" => "two", "a" => "three"); print_r($arr); /* Output: Array ( [a] => three [b] => two ) */ ?> |
If the key is omitted, PHP will generate integer keys beginning from 0, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = array(2, 3, 4); print_r($arr); /* Output: Array ( [0] => 2 [1] => 3 [2] => 4 ) */ ?> |
In the following example, since index 0 is defined twice, the latter value of ‘B’ is assigned to it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $arr = array('A', 0 => 'B', 'C'); print_r($arr); /* Output: Array ( [0] => B [1] => C ) */ ?> |
You can also create a 1-based array in PHP, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = array(1 => 'A', 'B', 'C'); print_r($arr); /* Output: Array ( [1] => A [2] => B [3] => C ) */ ?> |
The following example demonstrates how to create a two-dimensional array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php $arr = array ( "first" => array('one' => 1, 'two' => 2, 'three' => 3), "second" => array(2, 3, 4), "third" => array("a" => "one", "b" => "two", "a" => "three") ); print_r($arr); /* Output: Array ( [first] => Array ( [one] => 1 [two] => 2 [three] => 3 ) [second] => Array ( [0] => 2 [1] => 3 [2] => 4 ) [third] => Array ( [a] => three [b] => two ) ) */ ?> |
2. Using [] syntax
PHP 5.4+ offers the short array syntax [], eliminating the need for array() construct. It works similarly and avoids the overhead of calling a function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = ['one' => 1, 'two' => 2, 'three' => 3]; print_r($arr); /* Output: Array ( [one] => 1 [two] => 2 [three] => 3 ) */ ?> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = [2, 3, 4]; print_r($arr); /* Output: Array ( [0] => 2 [1] => 3 [2] => 4 ) */ ?> |
3. Using $arr[] = $val syntax
If you’re unsure of the contents of the array, you can create an empty array and push values onto the array later using the $arr[] = $val syntax.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $arr = []; $arr[] = "A"; $arr[] = "B"; $arr[] = "C"; print_r($arr); /* Output: Array ( [0] => A [1] => B [2] => C ) */ ?> |
To insert a key and value pair into an associative array, you can use the $arr[$key] = $val syntax:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $arr = []; $arr[1] = "one"; $arr[2] = "two"; $arr[3] = "three"; print_r($arr); /* Output: Array ( [1] => one [2] => two [3] => three ) */ ?> |
That’s all about creating and initializing an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)