PHP Array Overview

Array is a structured data which holds different types(string, array, object) of values and one or more values in a single variable, for example if you want to store 50 different types of values in a single variable instead of storing in 50 separate variables the array would help in that situation.

There is three different types of array available,

1. Numeric Array :

The array stored value's index will define by numbers, the index will be starting from zero.

Sample :

$values = array('arun','billa','chandru','david');
foreach ($values as $key $value) {
 print 'key - ' . $key . ' - value - ' . $value;
 print PHP_EOL;
}

Output :

key - 0 - value - arun
key - 1 - value - billa
key - 2 - value - chandru
key - 3 - value - david

2. Associative Array :

In the associative array we could define our own index and value in our style.

Sample :

$values = array('a' => 'arun', 'b' => 'billa', 'c' => 'chandru', 'd' => 'david');
foreach ($values as $key $value) {
 print 'key - ' . $key . ' - value - ' . $value;
 print PHP_EOL;
}

Output :

key - a - value - arun
key - b - value - billa
key - c - value - chandru
key - d - value - david

3. Multidimensional Array :

A multidimensional array's value might be an array that is called multidimensional array. and referred with the indexes.

Sample : 

$values = array('a' => array('a-1' => 'arun'), 'b' => array('b-1' => 'billa'), 'c' => array('c-1' => 'chandru'), 'd' => array('d-1' => 'david'));
foreach ($values as $key $value) {
 print 'key - ' . $key . ' - value - ' . $value[$key . '-1'];
 print PHP_EOL;
}

Output :

key - a - value - arun
key - b - value - billa
key - c - value - chandru
key - d - value - david
Tags: