PHP array
Arrays can store multiple values in a single variable:
Example
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
Run Example»
Click the "Run Example" button to view the online example
What is the array?
An array is a special variable that can store multiple values in a single variable.
If you have a list of items (for example: a list of car names), store it into a single variable like this:
$cars2="BMW";
$cars3="Toyota";
However, what if you want to iterate through the array and find a specific one? What if the array has not just 3 items but 300?
The solution is to create an array!
Arrays can store multiple values in a single variable, and you can access the values within them based on their keys.
What does php array mean?
php array, as the name suggests, is an array in PHP. Its characteristic is to map values to the type of keys. Unlike other languages, the keys of arrays in PHP can be strings, and the values can be of any type.
Creating arrays in PHP
In PHP, the array() function is used to create arrays:
In PHP, there are three types of arrays:
Numeric array - Array with numeric ID keys
Associative array - an array with specified keys, each key is associated with a value
Multidimensional array - Array containing one or more arrays
PHP numeric array
There are two ways to create numeric arrays:
Automatically assign ID keys (ID keys always start from 0):
Manually assigned ID key:
$cars[1]="BMW";
$cars[2]="Toyota";
The following example creates a numerical array named $cars, assigns three elements to the array, and then prints a text containing the array value:
Example
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
Run instance»
Click the "Run instance" button to view the online instance
Get the length of the array- count() function
count() function is used to return the length of the array (number of elements):
Example
<?php $cars=array("Volvo","BMW","Toyota"); echo count($cars); ?>
Run Instance»
Click the "Run Instance" button to view the online instance
Traverse the numeric array
To traverse and print all the values in the numeric array, you can use a for loop, as shown below:
Example
<?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); for($x=0;$x<$arrlength;$x++) { echo $cars[$x]; echo "<br>"; } ?>
Run instance»
Click the "Run instance" button to view the online instance
PHP associative array
Associative arrays are arrays using specified keys that you assign to the array.
There are two ways to create associative arrays:
or:
$age[' Ben']="37";
$age['Joe']="43";
The specified key can then be used in the script:
Instance
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>
Run instance»
Click the "Run instance" button to view the online instance
Traverse the association Array
To traverse and print all the values in the associative array, you can use a foreach loop as follows:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>
Run Example»
Click the "Run Example" button to view the online example
Multidimensional array
Multidimensional array will Detailed introduction in the PHP advanced tutorial section.
Complete PHP Array Reference Manual
For a complete reference manual of all common PHP array functions, please visit our PHP Array Reference Manual.
This reference manual provides a brief description and application examples of each function!