Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP array


May 11, 2021 PHP


Table of contents


PHP array

There are many functions about arrays in PHP that make array operation easy.


Arrays can store multiple values in a single variable:

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

Run an instance . . .

What is an array?

An array is a special variable that can store multiple values in a single variable.

If you have a list of items (ex: a list of car names), store it in a single variable, as follows:

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

However, what if you want to traverse the array and find a particular one? What if the array has not only 3 items but 300 items?

The solution is to create an array!

Arrays can store multiple values in a single variable, and you can access them based on keys.


Create an array in PHP

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

array();

In PHP, there are three types of arrays:

  • Numeric array - An array with a numeric ID key
  • Association Array - An array with a specified key, with one value associated with each key
  • Multi-dimensional array - An array that contains one or more arrays

An array of PHP values

There are two ways to create an array of values:

The ID key is automatically assigned (the ID key always starts at 0):

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

Manual allocation ID key:

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

The following example creates an array of values called $cars, assigns three elements to the array, and then prints a piece of text that contains the values of the array:

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

Run an instance . . .


Gets the length of the array - count() function

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

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

Run an instance . . .


Traverse the array of values

Traversing and printing all the values in the numeric array, you can use the for loop, as follows:

<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

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

Run an instance . . .


PhP association array

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

There are two ways to create an associated array:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

Or:

$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";

You can then use the specified keys in the script:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Run an instance . . .


Traverse the associated array

Traversing and printing all the values in the associated array, you can use the foreach loop, as follows:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

Run an instance . . .

Tip: The foreach syntax structure provides an easy way to traverse arrays. F oreach can only be applied to arrays and objects, and if you try to apply variables to other data types, or if you do not initialize variables, an error message is issued. There are two syntaxes:

foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement

The first format traverses a given array_expression array. In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward one step (so the next cell will be obtained in the next loop).

The second format does the same thing, except that the key name of the current cell is assigned to the variable in each loop$key.



Multi-dimensional array

Multi-dimensional arrays are covered in more detail in the PHP Advanced Tutorials section.


Complete PHP Array reference manual

To view the full reference manual for all array functions, please visit our PHP Array reference manual.

This reference manual provides a brief description of each function and an example of its application!