An array of a plurality of values can be stored in a single variable:
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
What arrays are?
An array is a special variable can store multiple values in a single variable.
If you have a list of items (for example: car name list), which is stored in a single variable, as follows:
$cars1="Volvo";
$cars2="BMW";
$cars3="Toyota";
However, if you want to loop through the array and find a specific one? If the array of items more than three but 300 it?
The solution is to create an array!
Arrays can store multiple values in a single variable, and you can access them according to the key value.
Create an array in PHP
In PHP, array () function is used to create an array:
array();
In PHP, there are three types of arrays:
Numeric array - the array with numeric keys ID
Associative array - with the specified array of keys, each key associated with a value
Multidimensional array - containing one or more arrays of arrays
PHP array of numbers
There are two ways to create a numeric array:
Automatically assigned an ID key (key ID is always starts from 0):
$cars=array("Volvo","BMW","Toyota");
Artificial assigned ID keys:
$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="Toyota";
The following example creates an array called value of $ cars, and assigned to the three elements of the array, and then print the text section contains an array of values:
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Get the length of the array - count () function
count () function returns the length of the array (number of elements):
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Traversing the array of numbers
All numerical values and traverse the print array, you can use a for loop, as follows:
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "
";
}
?>
PHP associative array
Associative array is specified using an array of key you assigned to the array.
There are two ways to create associative arrays:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
You can then use the specified key in the script:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Traversing associative arrays
Through and print all values in an associative array, you can use a foreach loop, as follows:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "
";
}
?>
Multidimensional Arrays
Multidimensional array will be described in detail in the advanced section of the tutorial PHP.
Complete PHP Array Reference
For a complete reference manual for all array functions, visit our PHP Array Reference Manual.
The reference manual provides a brief description of each function and application examples!
View all PHP tutorial article: https://www.codercto.com/courses/l/5.html (edit: Leilin Peng Source: network intrusion deleted)