PHP Arrays

An array can store one or more values in a single variable name.

Example

An array stores multiple values in a single variable:

<?php
$cars=array("porsche","BMW","Volvo");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Running Instances

What is an array?

An array is a special variable that can store more than one value at the same time.

If you have a list of projects (such as a list of car brands), storing these brand names in a single variable is like this:

$cars1="porsche";
$cars2="BMW";
$cars3="Volvo";

However, if you want to traverse a variable and find a specific value, or if you need to store 300 car brands instead of 3?

The solution is to create an array!

An array can store many values in a single variable name, and you can access a value by referencing the index number.

Creating an array in PHP

In PHP, array() The function is used to create an array:

array();

In PHP, there are three types of arrays:

  • Indexed Array - An array with numeric indices
  • Associated Array - An array with specified keys
  • Multidimensional Arrays - An array of one or more arrays

PHP Indexed Array

There are two ways to create an indexed array:

The index is automatically assigned (starting from 0):

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

Or you can manually assign the index:

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

The following example creates an indexed array named $cars, assigns three elements to it, and then outputs a paragraph containing the array values:

Example

<?php
$cars=array("porsche","BMW","Volvo");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Running Instances

Get the length of the array - count() function

count() The function is used to return the length of the array (number of elements):

Example

<?php
$cars=array("porsche","BMW","Volvo");
echo count($cars);
?>

Running Instances

Traverse the index array

To traverse and output all values of the index array, you can use a for loop, like this:

Example

<?php
$cars=array("porsche","BMW","Volvo");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++) {
  echo $cars[$x];
  echo "<br>";
}
?>

Running Instances

PHP Associative Arrays

An associative array is an array that uses the specified keys you assign to the array.

There are two ways to create associative arrays:

$age=array("Bill"=>"35","Steve"=>"37","Elon"=>"43");

Or:

$age['Bill']="63";
$age['Steve']="56";
$age['Elon']="47";

Subsequently, you can use the specified key in the script:

Example

<?php
$age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47");
echo "Elon is " . $age['Elon'] . " years old.";
?>

Running Instances

Traverse Associative Array

To traverse and output all values of an associative array, you can use a foreach loop, like this:

Example

<?php
$age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47");
foreach($age as $x=>$x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>

Running Instances

Multidimensional Arrays

We will explain PHP advanced tutorialsMultidimensional Arrays.

Complete PHP Array Reference Manual

For a complete reference manual of array functions, please visit our PHP Array Reference Manual.

This reference manual includes a brief description and usage examples for each function.