PHP Arrays

A PHP array is an ordered collection of data. PHP Arrays are very easy to create. PHP Arrays are dynamic – Items can be added and removed at will.

PHP Arrays can be created in two different ways

  • Using array() construct
  • Using array Operator []

PHP Arrays using Array Operator []

Array operator [] can be used to create an array in PHP. A general syntaxt of array operator for the creation of an array in PHP is:

$arrayName['key'] = value;

Here we are going to discuss two main types of PHP arrays using array operator – Numeric Arrays & Associative Arrays

PHP Numeric Arrays

Numeric arrays have their elements ordered by numbers (indexes). The first index of a numeric array is always 0.

PHP Associative Arrays

Associative arrays have their elements indexed with strings.

PHP Arrays using array() Construct

PHP uses array() construct to create an array. The structure of creation of array using array() construct is really simple, simply type array() and it will create an array. A general syntax for array() would be

array(key => value, key => value, key => value)

some examples of php array() constructs are given here

Count array elements:

To count the elements of an array you can use the count() function

More about Arrays:

Click here to read about PHP loops