You already know how variables work in PHP. You will now learn how to use arrays, in PHP.
As you code more and more, you will reach a point where you will need a great amount of variables and this is exactly what arrays provide. You can imagine them like a collection of variables, or an association of keys and values.
Remember:
- an array is named just like a variable
- it is made of several keys (also referred to as indexes) and values for each one
- each key is behaving just like a variable
-
Array definition
1 2 3 4 5 6 7 | <?php $arrayName = array("index1" => "value1", "index2" => 14, 10 => 14, 11 => "other value"); ?> |
where index1, index2, 10 and 11 are the keys, and are assigned their corresponding values:
– index1 -> value1
– index2 -> 14
– 10 -> 14
– 11 -> other value
To display the contents of the array, you can use the print_r() function. It will output all the keys and values in that array:
1 2 3 4 5 6 7 | <?php $arrayName = array("index1" => "value1", "index2" => 14, 10 => 14, 11 => "other value"); print_r($arrayName); ?> |
To add indexes to the array you can simply choose the key and add it’s value, just like any other variable assignment:
1 2 3 4 5 6 | <?php $arrayName = array("index1" => "value1", "index2" => 14, 10 => 14, 11 => "other value"); print_r($arrayName); ?> |
To display a value you can call it by placing the key between [ and ] after the array’s name.
1 2 3 4 5 6 7 8 9 | <?php $arrayName = array("index1" => "value1", "index2" => 14, 10 => 14, 11 => "other value"); echo $arrayName['index1']; echo $arrayName['index2']; echo $arrayName[10]; echo $arrayName[11]; ?> |
Notice: the text indexes are provided between ‘ or “, so the parser knows the key is type text, and calls the specific value in the array.
To add keys to the array, you can simply assign a value to the desired index
1 2 3 4 5 6 7 8 9 10 11 | <?php $arrayName = array("index1" => "value1", "index2" => 14, 10 => 14, 11 => "other value"); echo $arrayName['index1']; echo $arrayName['index2']; echo $arrayName[10]; echo $arrayName[11]; $arrayName["addedIndex"] = "some value"; ?> |
-
Array keys
Can be either a string, or an integer. You can define them any way you like.
1 2 3 4 5 6 7 8 | <?php $someVariable = "keyName"; $arrayName[$someVariable] = "some value"; echo $arrayName[$someVariable]; ?> |
-
Array values
An array element behaves just like a variable. It can store any type of data a variable can, and even store: another array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $firstArray = array(1 => "first value", 2 => array("test" => "some value", 12 => "value")); // when practicing, comment out some of // these outputs, to understand what happens to the arrays print_r($firstArray); // outputs both arrays print_r($firstArray[2]); echo $firstArray[1]; // first value echo $firstAray[2]["test"]; // some value echo $firstArray[12]; // value ?> |
The values in the secondary array can also be arrays themselves, so it is possible to create multidimensional arrays, simply by defining a key as a new array.
You can use arrays in various purposes: stacks, lists, queues, and much more.
Well done! You are one step closer to developing simple PHP applications.




