Home >Backend Development >PHP Problem >What are the php array types?
There are two types of php arrays, namely: 1. Index array, the subscript is composed of numbers, starting from 0 by default, each number corresponds to the position of an array element in the array; 2. Associative array, The subscript is composed of a mixture of numbers and strings. If a key name in an array is not a number, then the array is an associative array.
The operating system of this tutorial: windows11 system, PHP8.1.3 version, DELL G3 computer.
php array type
According to the different data types of array key names, we can divide PHP arrays into two types:
An array with numbers as keys is called an Indexed Array;
An array with strings or a mixture of strings and numbers as keys is called an Associative Array. ).
1. Index array
The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to The position of an array element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.
<?php header('content-type:text/html;charset=utf-8'); $array=array(1,2,3,4,5,6,7,8,9,10); var_dump($array);//打印数组 ?>
2. Associative array
The subscript (key name) of the associative array is composed of a mixture of numerical values and strings , if a key name in an array is not a number, then the array is an associative array.
<?php header('content-type:text/html;charset=utf-8'); $array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52); var_dump($array);//打印数组 ?>
The key name of an associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quote '' or double quote "". For indexed arrays, in order to avoid confusion, it is best to add delimiters.
Extended knowledge: mutual conversion between index array and associative array
Associative array to index array
In php, You can use the array_values() function to convert an associative array into an indexed array.
array_values($array) The function is to return the values of all elements in the array. It is very simple to use. With only one required parameter, it can return an array containing all the values in the given array, but not Key names are preserved. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.
Simply put, you can use this function to reset the array key name and convert the key name with confusing string or numerical value into a numeric key name starting from 0 and increasing by 1.
array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_values($arr)); ?>
Convert index array to associative array
In php, you can use the array_combine() function to convert the index array into an associative array array.
array_combine($keys,$values) function creates a new array by merging two arrays, where the elements in the $keys array serve as the keys of the new array, and the elements of the $values array serve as The key value of the new array.
But it should be noted that when using the array_combine() function to create an array, the number of elements in the $keys array and the $values array must be consistent, so that the key names and key values can correspond one to one, otherwise An error will be reported and FALSE will be returned.
And the $keys array cannot be a multi-dimensional array, otherwise an error will be reported; but the $values array can be a multi-dimensional array.
<?php header("Content-type:text/html;charset=utf-8"); $keys=array("a","b","c","d"); $values=array("red","green","blue","yellow"); var_dump($keys); var_dump($values); echo "使用array_combine()合并数组后:"; var_dump(array_combine($keys,$values)); ?>
The above is the detailed content of What are the php array types?. For more information, please follow other related articles on the PHP Chinese website!