Home > Article > Backend Development > How to change array index name in php
In PHP, you can use the array_combine() function to change the array index name. This function can use the element value of another array as the index (key) of the original array, thereby changing the index; the syntax "array_combine($ keys,$arr)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
How to change the array in php Index name
For example, there is an array like this:
$arr=array("red","green","blue","yellow");
How to modify its index name (key name)?
In PHP, you can use the array_combine() function to achieve this.
array_combine() function creates a new array by merging two arrays, where the elements of one array are key names and the elements of the other array are key values.
We only need to create a key name array, which contains the modified index name, and then the original array is used as the key value array, and the two arrays can be merged using array_combine().
Note: The number of elements in the key name array and the key value array must be the same!
Example:
The key name array is:
$keys=array("a","b","c","d");
Use the array_combine() function to replace the key name of the $arr array with the value of the $keys array:
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to change array index name in php. For more information, please follow other related articles on the PHP Chinese website!