PHP - Arrays

PHP - Arrays

The array is a variable that holds more than one value within a single variable. Generally, the element of the array will be of the same data type.

Kinds Of PHP Arrays

  • Indexed array: It is An array with a numeric index.
  • Associative array: It is also an array in which each key is associated with a value.
  • Multidimensional array: It is an array containing more than one array.

How To Declare PHP Array?

There are two ways to represent PHP arrays. These are:

  • Creating Array Using array() Function.
  • Creating Array using [ and ]

Let us see the associated example.

Source Code

Try it now

PHP Indexed Arrays

An indexed or numeric array is very useful for storing each array element with a numeric index. By default array index starts from 0 indexes and the last element index number is (array length-1).

To create an indexed array, pass multiple values inside the PHP array() function and separate each value with a comma ,.

General Syntax

Try it now

Source Code

Try it now

Remember: Indexes start from 0, not 1.

Index array element can be access through: $arrayName[index]

PHP Indexed Array Example

Source Code

Try it now

Associative Arrays

PHP associative array has named keys that contain the values.

Creating an associative array using two ways:

1:An associative array can be created using the array() function or [].

Source Code

Try it now