PHP Arrays

What are PHP Arrays

Arrays are advanced variables that enable storing multiple values or a collection of values under a single variable name. For example, if you need to store colors in your PHP script, individually assigning them to variables might appear like this:

<?php
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";
?>

Imagine you need to store the names of states or cities in a country, and not just a few but potentially hundreds. It would be tedious and impractical to store each city name in a separate variable. This is where arrays come into play.


Types of Arrays in PHP

PHP supports three types of arrays:

  • Indexed array — An array with numeric keys.
  • Associative array — An array where each key is associated with a specific value.
  • Multidimensional array — An array that contains one or more arrays within itself.

Indexed Arrays

An indexed or numeric array stores each element with a numeric index. Below are two ways to create an indexed array, with the simplest being:

<?php
// Define an indexed array
$colors = array("Red", "Green", "Blue");
?>

 

Note: In an indexed or numeric array, indexes are automatically assigned starting from 0, and the values can be of any data type.

This is similar to the following example, where indexes are assigned manually:

<?php
$colors[0] = "Red"; 
$colors[1] = "Green"; 
$colors[2] = "Blue"; 
?>

Associative Arrays

In associative arrays, values are assigned to keys, which can be arbitrary and defined by the user. The following example illustrates an array using keys instead of numeric indexes:

<?php
// Define an associative array
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);
?>

The following example achieves the same result as the previous one but demonstrates an alternative method of creating associative arrays:

<?php
$ages["Peter"] = "22";
$ages["Clark"] = "32";
$ages["John"] = "28";
?>

Multidimensional Arrays

A multidimensional array is an array where each element can also be an array. Each element in these sub-arrays can, in turn, be arrays themselves, creating nested structures. An example of a multidimensional array might look like this:

<?php
// Define a multidimensional array
$contacts = array(
array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
),
array(
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
),
array(
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
)
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>

Viewing Array Structure and Values

To view the structure and values of any array, you can use one of two functions: var_dump() or print_r(). However, print_r() provides less detailed information. Here's an example:

<?php
// Define array
$cities = array("London", "Paris", "New York");

// Display the cities array
print_r($cities);
?>

The output of print_r() is as follows:

Array ( [0] => London [1] => Paris [2] => New York )

This output displays both the key and the value for each element in the array. For more detailed information, you can use the following statement:

<?php
// Define array
$cities = array("London", "Paris", "New York");

// Display the cities array
var_dump($cities);
?>

The output of var_dump() is as follows:

array(3) { [0]=> string(6) "London" [1]=> string(5) "Paris" [2]=> string(8) "New York" }

This output includes the data type of each element, such as a string of 6 characters, in addition to displaying the key and value pairs.

In the next chapter, you will learn how to sort array elements.

In a later chapter, you will learn how to iterate through array values.