PHP Arrays


An array is a very special variable, which can hold more than one value at a time. It stores data items of similar types under a single identifier and hence simplify the data item management.

Array comes in use when we have to use multiple data items.

Suppose, we want to make a list of good cars manufacturer. So, generally how can one do:

$car1 = “Volvo”;
$car2 = “BMW”;
$car3 = “Toyoto”;
$car4 = “Ford”;
$car5 = “Tesla”;

But, with the help of an array we can store these all cars brand name in one variable.

$cars = ("Volvo", "BMW", "Toyoto", "Ford", "Tesla");

How to create an array:
Syntax- Array();


How to calculate the length of an array in php?

The count() function is used to calculate the length (number of elements) of an array.


In PHP, there are three types of array –

  1. Indexed array – array with numeric index
  2. Associative array – array with defined keys
  3. Multidimensional array – array containing one or more array

Indexed array

In indexed array, elements are represent by numeric number. By default, the index number starts from 0.

Indexed array can be created in two ways –

Let’s look below examples –

Elements are stored assuming first element at 0 index, second element at 1 index and so on.

$cars = array("BMW", "Toyoto", "Volvo");

Elements can be directly assign to the corresponding index numbers.

$cars[0] = "BMW";
$cars[1] = "Toyoto";
$cars[2] = "Volvo";

Loop through an Indexed array

We can print all elements/values of an array with the help of loop. Let’s understand this with an example-

Using for loop –

<?php
$cars = array("Volvo", "BMW", "Toyoto", "Ford", "Tesla");
$array_length = count($cars);

for($i=0; $i < $array_length; $i++)
{
   echo $cars[$i];
   echo "<br>";
}
?>

//Both will produce same output
Volvo
BMW
Toyota
Ford
Tesla

Using foreach –

<?php
$cars = array("Volvo", "BMW", "Toyoto", "Ford", "Tesla");

foreach($cars as $car)
{
   echo $car;
   echo "<br>";
}
?>

//Both will produce same output
Volvo
BMW
Toyota
Ford
Tesla

Associative array

In associative array, index or key are represented by a string. It is similar to indexed/numeric array. In indexed array, index is represented by numbers, whereas in this array string is used as indexing. This type of association is called key-value pair array.

You can create an associative array in two ways:

$cars = array(“Volvo” => “Black”, “BMW” => “White”, “Audi” => “Black”, “Ford” => “Red”);

Same array can be created as below :

$cars[“Volvo”] = “Black”;
$cars[“BMW”] = “White”;
$cars[“Audi”] = “Black”;
$cars[“Ford”] = “Red”;

Let’s understand with the help of example –

<?php
$cars = array("Volvo" => "black", "BMW" => "white", "Audi" => "black", "Ford" => "red");
echo "I have a BMW car of color  " .  $cars['BMW'] . "." ;
?>
//Output
I have a BMW car of color white

Loop through an Associative array

<?php
$cars = array("Volvo" => "black", "BMW" => "white", "Audi" => "black", "Ford" => "red");

foreach($cars as $key => $value)
{
  echo "M.S.Dhoni has " . $value . " colored " . $key;
  echo "<br>";
}
?>
//Output
M.S.Dhoni has black colored Volvo.
M.S.Dhoni has white colored BMW.
M.S.Dhoni has black colored Audi.
M.S.Dhoni has red colored Ford.

Multidimensional array

In multidimensional array, each element of the main array can also be an array. It is array in array.

Php supports two, three, four dimensional arrays etc. However it will become complex to manage higher dimensional arrays.


PHP – Two-dimensional arrays

A two dimensional array is an array of array for each element of main array. Let us understand with the below example of an employees details under a department of a company.

  • Employee name
  • Employee age
  • Employee salary
  • Employee department
  • Employee address

Here, for each employee you have create an detail array under main array of employees:

<?php
$employees = array(
   array('Melvin', 35, 35000, 'IT', 'Mumbai'),
   array('Sumant', 26, 55000, 'Operation', 'Noida'),
   array('Mayank', 40, 75000, 'Technology', 'Noida'),
   array('Brijesh', 55, 95000, 'IT', 'Bhopal')
);

echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "<th>Salary</th>";
echo "<th>Department</th>";
echo "<th>City</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

echo "<tr>";
echo "<td>" . $employees[0][0] . "</td>";
echo "<td>" . $employees[0][1] . "</td>";
echo "<td>" . $employees[0][2] . "</td>";
echo "<td>" . $employees[0][3] . "</td>";
echo "<td>" . $employees[0][4] . "</td>";
echo "</tr>";
    
echo "<tr>";
echo "<td>" . $employees[1][0] . "</td>";
echo "<td>" . $employees[1][1] . "</td>";
echo "<td>" . $employees[1][2] . "</td>";
echo "<td>" . $employees[1][3] . "</td>";
echo "<td>" . $employees[1][4] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>" . $employees[2][0] . "</td>";
echo "<td>" . $employees[2][1] . "</td>";
echo "<td>" . $employees[2][2] . "</td>";
echo "<td>" . $employees[2][3] . "</td>";
echo "<td>" . $employees[2][4] . "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>" . $employees[3][0] . "</td>";
echo "<td>" . $employees[3][1] . "</td>";
echo "<td>" . $employees[3][2] . "</td>";
echo "<td>" . $employees[3][3] . "</td>";
echo "<td>" . $employees[3][4] . "</td>";
echo "</tr>";
?>

//Output
Name    Age    Salary  Department      City
Melvin  35     35000   IT              Mumbai
Sumant  26     55000   Operation       Noida
Melvin  40     75000   Technology      Noida
Brijesh 55     95000   IT              Bhopal

Loop through two dimensional array.


<?php

$employees = array(
    array(
        'name' => 'Melvin',
        'age' => 35,
        'salary' => 35000,
        'dept' => 'IT',
        'city' => 'Mumbai'
    ),
    array(
        'name' => 'Sumant',
        'age' => 26,
        'salary' => 55000,
        'dept' => 'Operation',
        'city' => 'Noida'
    ),
    array(
        'name' => 'Melvin',
        'age' => 40,
        'salary' => 75000,
        'dept' => 'Technology',
        'city' => 'Noida'
    ),
    array(
        'name' => 'Brijesh',
        'age' => 55,
        'salary' => 95000,
        'dept' => 'IT',
        'city' => 'Bhopal'
    )
);

echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "<th>Salary</th>";
echo "<th>Department</th>";
echo "<th>City</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

foreach ($employees as $employee) {
    echo "<tr>";
    echo "<td>" . $employee['name'] . "</td>";
    echo "<td>" . $employee['age'] . "</td>";
    echo "<td>" . $employee['salary'] . "</td>";
    echo "<td>" . $employee['dept'] . "</td>";
    echo "<td>" . $employee['city'] . "</td>";
    echo "</tr>";
}

echo "</tbody>";
echo "</table>";
?>

//Output
Name    Age    Salary  Department      City
Melvin  35     35000   IT              Mumbai
Sumant  26     55000   Operation       Noida
Melvin  40     75000   Technology      Noida
Brijesh 55     95000   IT              Bhopal

Trending Topics