PHP Arrays: Working with Indexed and Associative Arrays

Olivia J
5 min readMay 22, 2023

--

Arrays are one of the most useful data structures in PHP. They allow you to store multiple values in a single variable and access them by using numeric or string keys. In this blog post, we will learn how to create and manipulate indexed and associative arrays in PHP.

Photo by Ben Griffiths on Unsplash

What are Indexed Arrays?

An indexed array is an array that uses numeric keys to store and retrieve values. The keys are automatically assigned and start from 0. You can create an indexed array by using the array() function or the short array syntax []. For example:

// Using the array() function
$colors = array("Red", "Green", "Blue");

// Using the short array syntax
$colors = ["Red", "Green", "Blue"];

You can access the values of an indexed array by using the square bracket notation with the key. For example:

echo $colors[0]; // Prints "Red"
echo $colors[1]; // Prints "Green"
echo $colors[2]; // Prints "Blue"

You can also modify the values of an indexed array by assigning a new value to a key. For example:

$colors[0] = "Pink"; // Changes the first element to "Pink"
$colors[3] = "Yellow"; // Adds a new element at the end

What are Associative Arrays?

An associative array is an array that uses named keys to store and retrieve values. The keys can be any strings that you define. You can create an associative array by using the array() function or the short array syntax [] with the key => value pairs as arguments. For example:

// Using the array() function
$ages = array("Peter" => 22, "Clark" => 32, "John" => 28);

// Using the short array syntax
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];

You can access the values of an associative array by using the square bracket notation with the key. For example:

echo $ages["Peter"]; // Prints 22
echo $ages["Clark"]; // Prints 32
echo $ages["John"]; // Prints 28

You can also modify the values of an associative array by assigning a new value to a key. For example:

$ages["Peter"] = 23; // Changes Peter's age to 23
$ages["Lisa"] = 25; // Adds a new element with key "Lisa" and value 25

How to Loop Through Arrays?

One of the most common tasks when working with arrays is to loop through them and perform some actions on each element. PHP provides several ways to loop through arrays, such as for, foreach, while, and do-while loops.

The simplest way to loop through an indexed array is to use a for loop with a counter variable that goes from 0 to the length of the array minus one. For example:

// Loop through an indexed array using a for loop
$colors = ["Red", "Green", "Blue"];

for ($i = 0; $i < count($colors); $i++) {
echo $colors[$i] . "<br>";
}

The output of this code will be:

Red
Green
Blue

The simplest way to loop through an associative array is to use a foreach loop that iterates over each key-value pair in the array. For example:

// Loop through an associative array using a foreach loop
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];

foreach ($ages as $key => $value) {
echo "$key is $value years old.<br>";
}

The output of this code will be:

Peter is 22 years old.
Clark is 32 years old.
John is 28 years old.

You can also use other types of loops to iterate over arrays, such as while and do-while loops, but they require more code and are less efficient than for and foreach loops.

How to Sort Arrays?

Another common task when working with arrays is to sort them according to some criteria, such as alphabetical order, numerical order, or custom order. PHP provides several built-in functions to sort arrays, such as sort(), rsort(), asort(), arsort(), ksort(), and krsort().

The sort() function sorts an indexed array in ascending order according to the values. The keys are renumbered starting from 0. For example:

// Sort an indexed array in ascending order using sort()
$colors = ["Red", "Green", "Blue"];
sort($colors);
print_r($colors);

The output of this code will be:

Array ( [0] => Blue [1] => Green [2] => Red )

The rsort() function sorts an indexed array in descending order according to the values. The keys are renumbered starting from 0. For example:

// Sort an indexed array in descending order using rsort()
$colors = ["Red", "Green", "Blue"];
rsort($colors);
print_r($colors);

The output of this code will be:,

Array ( [0] => Red [1] => Green [2] => Blue )

The asort() function sorts an associative array in ascending order according to the values. The keys are preserved. For example:

// Sort an associative array in ascending order using asort()
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];
asort($ages);
print_r($ages);

The output of this code will be:

Array ( [Peter] => 22 [John] => 28 [Clark] => 32 )

The arsort() function sorts an associative array in descending order according to the values. The keys are preserved. For example:

// Sort an associative array in descending order using arsort()
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];
arsort($ages);
print_r($ages);

The output of this code will be:

Array ( [Clark] => 32 [John] => 28 [Peter] => 22 )

The ksort() function sorts an associative array in ascending order according to the keys. The values are preserved. For example:

// Sort an associative array in ascending order using ksort()
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];
ksort($ages);
print_r($ages);

The output of this code will be:

Array ( [Clark] => 32 [John] => 28 [Peter] => 22 )

The krsort() function sorts an associative array in descending order according to the keys. The values are preserved. For example:

// Sort an associative array in descending order using krsort()
$ages = ["Peter" => 22, "Clark" => 32, "John" => 28];
krsort($ages);
print_r($ages);

The output of this code will be:

Array ( [Peter] => 22 [John] => 28 [Clark] => 32 )

There are also other functions that can sort arrays based on custom comparison functions or flags, such as usort(), uasort(), uksort(), and array_multisort(). You can learn more about them from the official PHP documentation.

In this blog post, we have learned how to create and manipulate indexed and associative arrays in PHP. We have also learned how to loop through arrays and sort them using various functions. Arrays are very powerful and versatile data structures that can help you store and process complex data in your PHP scripts.

--

--