PHP arrays
PHP has two types of arrays: indexed arrays, associative arrays.
Brief introduction
An indexed array is an array of exponential groups whose keys are integers, and the integer order of the keys starts at 0, and so on. As shown in the following code:
$fruit=array("Apple", "banana", "pineapple");
Assign value
The
- assigns a value with the name of an array variable followed by a square bracket, and of course, the key in the index array, within the brackets, must be an integer. For example,
$arr [0]= ' Apple ';
- use
array ()
to create an empty array, using =>
symbols to separate keys and values, the left for the key, and the right to represent values. Of course, in an indexed array, the key must be an integer. For example, array (' 0 ' = ' apple ');
- use
array ()
to create an empty array, directly in the array in English single quotation marks or double quotation marks in English ", the array defaults to the key of the integer starting from 0. such as array (' Apple ');
This array is equivalent to array (' 0 ' = ' apple ');
<? PHP $arr=array();//Create an empty array for ($i=0; $i<10; $i+ +) {//loop traversal, assigning a value to the array $arr[$i]=$i+1; } Print_r ($arr);? >
Accessing the value of an array
<? PHP // from the array variable $arr, read the value of the key 0 $arr Array (' apples ', ' bananas '); Print_r ($arr[' 0 ']);? >
- For iterating through the values in the array
<? PHP Header ("content-type:text/html; Charset=utf-8 "); $fruit=array(' Apple ', ' banana ', ' pineapple '); for ($i= 0; $i<count($fruit); $i+ +) {echo ' <br> array '. $i. ' The value is: '. $fruit [$i]; }? >
2.foreach iterating through the values in the array
<? PHP Header ("content-type:text/html; Charset=utf-8 "); $fruit=array(' Apple ', ' banana ', ' pineapple '); foreach ($fruitas$key=$value) { echo ' <br > '. $key. ' The value is: '. $value ; }? >
An associative array is an array of exponential groups whose keys are strings.
<? PHP Header ("content-type:text/html; Charset=utf-8 "); $fruit=arr ( ' apple ' = ' apple ', ' banana ' + ' banana ', ' pineapple ' = ' pineapple ') );? >
Assign value
- Assign a value with the name of the array variable followed by a square bracket, of course, the key in the associative array, in parentheses, must be a string. Like what
$arr[‘apple‘]=‘苹果‘;
array()
creates an empty array, uses =>
symbols to separate keys and values, the left represents the key, and the right represents the value. Of course, the key must be a string in the associative array. Like whatarray(‘apple‘=>‘苹果‘);
Accessing the value of an associative array
<? PHP // from the array variable $arr, read the value of the key to Apple $arr Array (' apple ' = ' apple ', ' banana ' and ' banana ', ' pineapple ' and ' pineapple '); Print_r ($arr[' Apple ']); >
A Foreach loop accesses the value of an associative array
<? PHP Header ("content-type:text/html; Charset=utf-8 "); $fruit=array(' apple ' = ' apple ', ' banana ' = ' banana ', ' pineapple ' = ' pineapple '); foreach ($fruitas$key=$value) { echo ' <br > '. $key. ' The corresponding Chinese is '. $value ; }? >
PHP Basics-Arrays