Home  >  Article  >  Backend Development  >  How to change the value of array key in php

How to change the value of array key in php

青灯夜游
青灯夜游Original
2022-07-01 19:57:593883browse

Two ways to change: 1. Use the array_values() function to reset the key name of the array. The syntax "array_values($array)" is suitable for arrays with associated functions or confusing key names. , turning it into a numeric value starting from 0 and increasing by 1. 2. Use the array_combine() function to change the key of the array. The value of one array can be used as the key of another array. The syntax is "array_combine(key array, original array)".

How to change the value of array key in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php changes the value of the array key, that is, changes the array key name. There are two methods:

  • Use the array_values() function

  • Use the array_combine() function

Method 1: Use the array_values() function to reset the key name of the array

The array_values() function can obtain the values ​​of all elements in the array. The syntax format of this function is as follows:

array_values($array)
  • The parameter $array is the array being operated on.

array_values() function is particularly suitable for arrays with confusing element subscripts or associative arrays.

array_values() function can return an array containing all the values ​​in the given array, but does not retain the key names. That is, the returned array will be in the form of an indexed array. The index of the array starts from 0 and increases by 1.

Example:

<?php
$arr1=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr1);
var_dump(array_values($arr1));

$arr2=array(2=>65,8=>80,5=>78,0=>90);
var_dump($arr2);
var_dump(array_values($arr2));
?>

How to change the value of array key in php

Method 2: Use the 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.

It should be noted that when using the array_combine() function to create 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 be consistent. One corresponding, otherwise an error will be reported and FALSE will be returned.

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");
var_dump($keys);
echo "使用array_combine()后:";
$arr=array_combine($keys,$arr);
var_dump($arr);
?>

How to change the value of array key in php

Recommended learning: "PHP Video Tutorial"

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