PHP Array Tutorial

An array is a data structure or a rather a collection of data that contains a group of elements. Typically these elements are all of the same data types or different data types.

for example:
If a complex has thirty flats each flat having same features then the whole complex is considered to be an Array.
Each of thirty flat is an element of that array and as each flat shares the same type of features – those are considered as same data types.

So, in a programming language like PHP, the array is a collection of different type of value in a single variable. You also can say a string is one kind of array because string consists of multiple characters.

Note: In programming languages like ‘C’, only one type of value (integer or string or float etc.) can be stored in an array and must be defined at the beginning of the program, but in PHP we can store different types of value (string, integer, Boolean, null, object, float) in a single array.

define an array
An array is a data structure that stores one or more different type of values in a single variable.

Syntax of an array

Note: Most of developers use syntax type II for robust programming support.

print an array

Note: this two functions (print_r() and var_dump()) are also debugging function in PHP (about debugging function will be discussed later)


now 0 1 2 3 are index of the value means 0 1 2 3 in this position apple mango banana lemon are stored respectively.
Index means which is used to identify the particular one value/element in the array.

We can find a house by its address but when we try to find a room in an apartment there must be a number or floor division which separates every room/house from each other rooms/houses.

In similar way multiple value stores in single array separates by a unique index number. It is also called an ‘ARRAY IDENTIFIRE’. Index, always starts from 0 and automatically incremented, but you also set index manually.

You can return particular one element of any array by using index number of this array.

In another way you also define array in PHP:

Display an array

This is the example of manual index value. We can also use in this manner.


How to know the length or size of an unknown array


Printing array using loop in PHP


Using FOREACH loop