PHP Arrays

An array is an indexed list of values separated by commas. Arrays in PHP are similar to arrays in Javascript. They can also contain different data types include strings, numbers, and other arrays.

In PHP, arrays can be defined in two ways: using the array() function or using the short array syntax which uses a set of square brackets ([]).

NOTE

The short array syntax ([]) was introduced in PHP 5.4, which was released in March 2012. The short array syntax is the preferred way of creating arrays.

<?php
  // using the array() function
  $array1 = array();

  // using the short array syntax
  $array2 = [];
?>

Indexed Arrays

By default, arrays are indexed using numbers. Each value in an array receives an index starting with the number 0.

An index is used to retrieve a value from the array. This is accomplished by calling the variable name of the array followed by a set of square brackets ([]). Inside the square brackets is the index of the desired value.

<?php
  $numbers = [4, 8, 15, 16, 23, 42];
  
  // getting the second item
  echo $numbers[1]; // 8

  // getting the first item
  echo $numbers[0]; // 4
?>

Arrays may contain any data type and arrays can have a mix of data types as well.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]]; 
  echo $mixed[2]; // "dog"
?>

Displaying Arrays

One common mistake most beginners make when working with arrays is attempting to output an array to the page using echo. But this will often produce an unexpected result. Instead of displaying the contents of an array, the word "Array" will be displayed followed by an array to string conversion warning.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]]; 
  echo $mixed; // Array
?>

To properly output an array using echo, the print_r() function should be used.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]]; 
  print_r($mixed); // human-readable array
?>

Adding <pre></pre> around the PHP will produce a well-formatted output of the array.

<pre>
<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]];  
  print_r($mixed); // human-readable array
?>
</pre>

Nested Arrays

When retrieving a value from a nested array, it is necessary to add an additional set of square brackets.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]]; 
  echo $mixed[3][1]; // "y"
?>

Updating Arrays

Once an array has been defined, it is possible to change a value in the array.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]]; 
  
  // changing the array value
  $mixed[2] = "cat";
?>

Additional values can be similarly added to the array by calling the next index in the array or by providing no index at all.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]];  
  
  // adding the value to the number 4 index
  $mixed[4] = "mouse";

  // adding the value to the end of the array
  $mixed[] = "horse";
?>

Values can be removed from the array using the array_splice() function.

<?php 
  $mixed = [6, "fox", "dog", ["x", "y", "z"]];  
  
 // removing values from the array using array_splice
  array_splice($mixed, 1, 1);
?>