PHP Array

Whenever we want to store related data like list of books, list of students etc at one place, we use an array. An array not only allows to store related data but to organise it as you like and also quickly access to that data. Array can be effectively manipulated. In PHP, arrays are indexed from 0 and not from 1.

PHP has three types of arrays

  • Indexed or Numeric Array
  • Associative Array
  • Multidimentional Array

Array can be specified in following two different ways

  • With the use of array() construct
  • With the use of array operator[]

Specifying an array using array() construct

Syntax

PHP Array Example 1

This will produce following result

Specifying an array using array operator[]

Syntax:

PHP Array Example 2

This will produce following result

To print a list of an array

This can be done using implode() function.

PHP Print a List of an Array Example

This will produce following result

Print a Comma Separated List of an Array

This can also be done using implode() function but differntly.

Print a Comma Separated List Example

This will produce following result

Reverse an Array

This can be done using array_reverse() function.

Reverse an Array Example

This will produce following result

Process More than one Element of an Array

This can be done using array_slice() function.

Process Array Elements Example

This will produce following result

Trim an Array

This can be done using array_splice() function.

Trim an Array Example

This will produce following result

Count number of Elements in an Array

This can be done using count() function.

Count Array Elements Example

This will produce following result