Home  >  Article  >  Backend Development  >  How to replace array key name in php

How to replace array key name in php

王林
王林Original
2020-08-27 14:32:294549browse

How to replace array key names in php: You can use the array_combine() function to achieve replacement. The array_combine() function can create 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.

How to replace array key name in php

We can use the array_combine() function to achieve this, which creates a new array by merging two arrays, one of which is the key name. The elements of the other array are key values ​​and the merged array is returned. If the two arrays have different numbers of elements, return FALSE.

(Recommended tutorial: php video tutorial)

Note: The number of elements in the key name array and the key value array must be the same!

Syntax:

array_combine(keys,values);

Parameters:

  • ##keys Required. Specifies the key name of the array

  • #values ​​Required. Specify the key value of the array

(Related recommendation:

php training)

Code example:


<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>

Output Result:


Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )

The above is the detailed content of How to replace array key 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