There are ways of assigning values to arrays: by association and by indexing.

The indexing method

An indexed array is referred to the element number of an array. If an element in an array is to be altered or shift, its assigned index will be referred in the code. The example below how the indexing done:

Syntax for arrays indexing method

$array[index] = value_1;

The associative method

The associative method of approaching arrays is a way in which each element in the array is assigned a name (key) which refer to the value. The example below how the association done:

Syntax for arrays associative method

array (
    key_1 => value_1,
    key_2 => value_2,
    ...
    key_n => value_n,
);

Looping through arrays

To loop through an array and read all or some values, we can use following techniques: a simple index searching loop, or a key = > value associative loop; the former using a FOR conditional statement, while the latter using a FOREACH statement.

Example of a simple index searching loop through array

Example of an associative loop through array

 

›› go to examples ››