PHP tutorial A Quick Guide To Working With Arrays

An array is a variable, which can hold multiple values in it. Take it as a cupboard with multiple compartments within it. The cupboard has compartments, and each compartment holds something.


Arrays are useful when you need to store values that are related, and must be kept close for organizing purpose. Such as, the list of colors, list of children with age and gender, list of countries and their capital, etc.

There are 3 types of arrays in PHP. Let’s discuss each in detail.

1. Indexed Array

Indexed array stores multiple elements and assigns them a numeric identifier called index, starting from 0.

For example, the list of countries in indexed array will be assigned like this:


$countries = array("Japan", "China", "Pakistan", "England");


In the above example, we used PHP’s array() function to create an array, and automatically assigned index to it. The index can also be manually assigned like this:

$countries[1] = "China";
$countries[2] = "Pakistan";
$countries[3] = "England";

Quite simple, isn’t it?

Now let’s print the array. There are 2 common ways to do it. Printing it Manually and Printing it through a Loop.

Printing - The Manual Way

echo $countries[0];
echo "<br>The second country is: ".$countries[1].". ";
echo "Then come $countries[2] and $countries[3].";


The second country is: China. Then come Pakistan and England.

Did you notice that the countries array is showing 4th country on index 3. This is because it is starting at 0. Memorize it and it will help you in future coding.

Printing - Through a Loop

foreach($countries as $c) {
    echo $c."<br>";



foreach() is simple and easy to loop through an array. You can also use for, while, and do-while here, but then you will have to get the total count of the array. Which you can also do using count() function. Example code:

$count = count($countries);
echo $count;

Output: 4

Let's move over to the next type of array.

2. Associative Array

The main difference between Indexed array and Associative array is that the Indexed array used numeric integer as an index, while Associative array uses string/alphanumeric words as an index, also known as keys.

The best use of Associative array can be in username/password list as an array. Unfortunately, it can only store 1 value for each index. If you would like to store password, age, name, and location etc, you would need to look into multi-dimensional array, which we will talk about later.

Associative array can be created using 2 methods.

1. Array() Method

$users = array("Adam"=>"Miami", "John"=>"London", "Ali"=>"Tokyo");

In the above example, we set the location of each user. Notice how it is similar to creation of indexed array. Difference is the declaration of of values followed by "=>", followed by keys.

2. Separately Assign Each Value

$users["Adam"] = "Miami";
$users["John"] = "London";
$users["Ali"] = "Tokyo";

Associative array can also be assigned manually by assigning values to each key separately.

Printing the associative array is also similar to printing indexed array, but with a little change in loop method. Let's check them out here:

1. Printing Associative Array - Manually

echo "Ali lives in ".$users["Ali"]."!";

//Output: Ali lives in Tokyo!

2. Printing Associative Array - Through a Loop

foreach($users as $k => $v) {
    echo $k." Lives in ".$v."! ";

//Output: Adam Lives in Miami! John Lives in London! Ali Lives in Tokyo!

The next step is multidimensional array. Check out the next tutorial for multidimensional array.


over 1 year ago

As of PHP 5.4 you can use short array syntax ($countries = ["Japan", "China"];):


Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?