PHP Array

PHP Array is an array that stores one or more types of values in a single array. In other words An array is a special variable, which can hold more than one value at a time. For example if you want to store 50 names then instead of defining 50 variables its easy to define an array of 50 length. In array’s there are three different types of arrays. We will see these in more details. These 50 values in an array, we will access them using ID’s which is called array index.

We will define 50 names using variables like this.

$name1 = 'Raju';
$name2 = 'Ramu';
$name3 = 'Rani';
$name50 = 'Bunny';

Above data can be used in a single array like this.

$names = array('Raju', 'Ramu', 'Rani', ......., 'Bunny');

We can access this data by using array index ID’s.

echo $names[0];
echo $name[1];
echo $name[50];

There are three different types of arrays.

  • Indexed arrays – Arrays with numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

Indexed arrays or Numeric Index array in PHP

An array with a numeric index. Values are stored and accessed in linear fashion, And example of Numeric Index array is

$names = array('Raju', 'Ramu', 'Rani', ......., 'Bunny');

Loop Through an Indexed Array in PHP

I’m taking above array as an example with four values. For looping through an array I’ll use foreach function in PHP.

$names = array('Raju', 'Ramu', 'Rani', 'Bunny');
foreach ($names as $name) {
    echo "My name is" . $name;

Associative arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the ages of persons in an array, a numerically indexed array would not be the best choice. Instead, we could use the person names as the keys in our associative array, and the value would be their respective ages.

There are two ways to create an associative array.


And the other method to create an associative array is:


Loop Through an associative Array in PHP

Here I’ll loop through $age array. For looping through an array I’ll use foreach function in PHP.


foreach ($age as $key => $value) {
    echo "Age of $key is $value<br />\n";

PHP Multidimensional Arrays

Array does not have to be a simple list of keys and values; each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Check Your Website Rankings

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: