CHAPTER 5

image

Arrays

An array is used to store a collection of values in a single variable. Arrays in PHP consist of key-value pairs. The key can either be an integer (numeric array), a string (associative array) or a combination of both (mixed array). The value can be any data type.

Numeric arrays

Numeric arrays store each element in the array with a numeric index. An array is created using the array constructor. This constructor takes a list of values which are assigned to elements of the array.

$a = array(1,2,3);

As of PHP 5.4 a shorter syntax is available, where the array constructor is replaced with square brackets.

$a = [1,2,3];

Once the ...

Get PHP Quick Scripting Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.