Home > Backend Development > PHP Problem > How to change the key name of an array in php

How to change the key name of an array in php

青灯夜游
Release: 2023-03-13 18:36:01
Original
3367 people have browsed it

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

How to change the key name of an array in php

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);
Copy after login
  • $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);
?>
Copy after login

Output result:

1 (1).png

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template