PHP Associative Array

Associative Array performs same function as that of Indexed Array but unlike Indexed Array which uses Numeric Index, Associative Array uses a String Index for referring to an element of array. In PHP, if you want to create an associative array, you are required to use the => operator.

Associative Array can be specified in following two different ways

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

Associative Array Using Array() Construct

Syntax

Associative Array Using Array() Example

This will produce following result

Associative Array Using Array operator[]

Syntax

Associative Array Using Array operator[] Example

This will produce following result:

Count Number of Elements in an Associative Array

In PHP Associative Array, array count can be done using count() function.

Count Number of Elements in an Associative Array Example

This will produce following result

Display elements in an Associative Array using Foreach

In PHP Associative Array, array elements can be display using Foreach.

Associative Array using Foreach Example

This will produce following result