Home>Article>Backend Development> How to change array index name in php

How to change array index name in php

青灯夜游
青灯夜游 Original
2022-02-17 14:03:04 2355browse

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)".

How to change array index name in php

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 change array index name in php

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:

       

How to change array index name in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn