Home > Article > Backend Development > How to change the key name of an array in php
In PHP, you can use the array_combine() function to change the key of an array. This function can use the value of one array as the key name of another array; syntax "array_combine(key name array, original array) ".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_combine () function to change the key of an array.
array_combine() function creates a new array by merging two arrays, one of which is the key name, and the value of the other array is the key value.
Using this feature, the value of one array can be used as the key name of another array.
Syntax:
array_combine($keys,$values);
$keys Required. Array of key names.
#$values Required. Key-value array.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("red","green","blue","yellow"); var_dump($arr); $keys=array("a","b","c","d"); echo "使用array_combine()后:"; $arr=array_combine($keys,$arr); var_dump($arr); ?>
Output result:
It should be noted that using array_combine( ) function creates an array, the number of elements in the $keys
array and the $values
array must be consistent, so that the key name and key value can correspond one to one, otherwise an error will be reported and Return FALSE.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to change the key name of an array in php. For more information, please follow other related articles on the PHP Chinese website!