PHP array basic operation summary (recommended), basic operation summary
Array Concept
Array is a very important concept in PHP. we can regard arrays as a collection of similar data. In fact, arrays are an ordered graph.
PHP also provides over 70 built-in functions to operate arrays.
Create an array
Use the array () language structure to create an array:
<? Php $ arr_age1 = array (18, 20, 25); // or: $ arr_age2 = array ("wang" => 18, "li" => 20, "zhang" => 25); // empty array: $ arr_age3 = array ();?>
You can also use the array controller [] to create an array:
<? Php $ arr_age1 [] = 18; $ arr_age1 [] = 20; $ arr_age1 [] = 25; // or: $ arr_age2 ["wang"] = 18; $ arr_age2 ["li"] = 20; $ arr_age2 ["zhang"] = 25;?>
Array key name and Value
The array object contains two items: key name and value.
In the example of creating an array below:
$ Arr_age1 = array (18, 20, 25); we assigned $ arr_age1 three array units (also called elements) with values of 18, 20, and 25. The system automatically assigns three numbers to the three array units, which are 0, 1, and 2. That is, the complete structure of the $ arr_age1 array is:
Array ([0] => 18 [1] => 20 [2] => 25) the sequence number automatically assigned by the system is called the key name, this type of key-name array is called the Index array (indexed array ).
You can also manually specify the key name:
$ Arr_age1 = array (0 => 18, 1 => 20, 2 => 25 );
Note: manually specifying a key name does not start from 0, or you do not need to specify a key name in numerical order. When a new unit is added to an array without specifying the health name, the system automatically adds 1 to the largest numeric key in the existing array as the key name of the new unit.
When a string instead of a numeric index is used as the key name, this array is called the associative array ):
$ Arr_age2 = array ("wang" => 18, "li" => 20, "zhang" => 25); but in PHP, the two arrays have no obvious boundary and can be used together. Note that the associated array is case sensitive to key names.
Output array unit value
You can use the following method to access the output array unit value:
Echo $ arr_age1 [0];
// Output: 18
Echo $ arr_age2 ["wang"];
// Output: 18 in some cases, the data and structure of the entire array may need to be output for debugging. In this case, the print_r () or var_dump () function must be used, for more information, see PHP print_r and var_dump output arrays.
Operation Array Unit
Array units can be operated like normal variables, for example:
<?php$arr_age2 = array("wang"=>18, "li"=>20, "zhang"=>25);$arr_age2["wang"] = $arr_age2["wang"] + 10;?>
Now $ arr_age2 is:
Array ([wang] => 28 [li] => 20 [zhang] => 25)
Check whether an array unit is set. Use isset ().
Destroy An Array
Use the unset () function to destroy an array unit or the entire array:
<?phpunset($arr_age1[0]);unset($arr_age1);?>
Multi-dimensional array
If the value in the array is also an array, we call this array a recursive array or a multi-dimensional array.
Example:
<? Php $ all = array ("fruits" => array ("a" => "orange", "B" => "banana ", "c" => "apple"), "ages" => array (18, 20, 25); echo $ all ["fruits"] ["c"]; // output appleecho $ all ["ages"] [0]; // output 18?>
The above PHP array basic operation summary (recommended) is all the content shared by the editor. I hope to give you a reference and support for the help house.