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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
