Home > Backend Development > PHP Problem > How to change the key name or index of a PHP array

How to change the key name or index of a PHP array

PHPz
Release: 2023-04-19 10:50:13
Original
679 people have browsed it

In PHP, array is a very commonly used data type. We can usually access elements in an array using index or key names. In some cases, we may need to change the key name or index of an element. This article will introduce how to change the key name or index of a PHP array.

1. Change the index of the array

1. Use the PHP function array_values()

If you want to change the index of the array, you can use the PHP built-in function array_values(). This function will return a new index array with indexes incrementing from 0.

Sample code:

// 原数组
$fruits = array("apple", "banana", "cherry");
 
// 使用array_values()函数获取新的索引数组
$new_fruits = array_values($fruits);
 
// 输出新数组
print_r($new_fruits);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)
Copy after login

In the sample code, we first declare a primitive array containing three elements: $fruits. Then, we use the array_values() function to get a new index array $new_fruits from the original array. Finally, we use the print_r() function to output the new array.

2. Use the PHP function array_combine()

If you want to use the value in the original array as the key name of the new index array, you can use the array_combine() function. This function requires two parameters, one as the key name of the new index array, and one as the value of the original array.

Sample code:

// 原数组
$fruits = array("apple", "banana", "cherry");
 
// 使用array_combine()函数获取新的索引数组
$new_fruits = array_combine(range(1, count($fruits)), array_values($fruits));
 
// 输出新数组
print_r($new_fruits);
Copy after login

Output result:

Array
(
    [1] => apple
    [2] => banana
    [3] => cherry
)
Copy after login
Copy after login

In this sample code, we first declare a primitive array $fruits containing three elements. We then use the range() function to generate a new array containing the index keys 1, 2, and 3. Finally, we use the array_values() function to get the values ​​of the original array and use the array_combine() function to combine the two arrays into a new indexed array $new_fruits.

3. Use a foreach loop to traverse the array

Changing the index of the array can also be achieved by traversing the array and manually changing the index of each element.

Example code:

// 原数组
$fruits = array("apple", "banana", "cherry");
 
// 遍历数组并更改每个元素的索引
$new_fruits = array();
$i = 1;
foreach ($fruits as $fruit) {
    $new_fruits[$i] = $fruit;
    $i++;
}
 
// 输出新数组
print_r($new_fruits);
Copy after login

Output result:

Array
(
    [1] => apple
    [2] => banana
    [3] => cherry
)
Copy after login
Copy after login

In this example code, we declare a primitive array $fruits containing three elements. We then use a foreach loop to iterate through the array and change the index of each element to 1, 2 and 3 and store it in a new array $new_fruits. Finally, we use the print_r() function to output the new array.

2. Change the key name of the array

1. Use the PHP functions array_keys() and array_values()

If you want to change the key name of the array, you can use the PHP function array_keys() and array_values().

Sample code:

// 原数组
$student_scores = array("Tom" => 80, "Jerry" => 90, "Mickey" => 95);
 
// 获取新的键名数组和值数组
$new_keys = array_values($student_scores);
$new_values = array_keys($student_scores);
 
// 用新键名数组和值数组组成新数组
$new_student_scores = array_combine($new_keys, $new_values);
 
// 输出新数组
print_r($new_student_scores);
Copy after login

Output result:

Array
(
    [80] => Tom
    [90] => Jerry
    [95] => Mickey
)
Copy after login
Copy after login

In this sample code, we declare an array $student_scores containing three original key-value pairs. Then, we use the array_keys() function to get the keys of the original array, and the array_values() function to get the values ​​of the original array. Next, we use the array_combine() function to recombine the new keys and values ​​into a new array $new_student_scores.

2. Use the PHP function array_map()

You can also use the PHP function array_map() to change the key name of the array. The array_map() function applies a callback function to an array and returns a new array containing all return values.

Sample code:

// 原数组
$student_scores = array("Tom" => 80, "Jerry" => 90, "Mickey" => 95);
 
// 使用array_map()函数更改键名
$new_student_scores = array_map(function ($score, $name) {
    return [$score, $name];
}, $student_scores, array_keys($student_scores));
 
// 用新键名数组和值数组组成新数组
$new_student_scores = array_combine(array_column($new_student_scores, 0), array_column($new_student_scores, 1));
 
// 输出新数组
print_r($new_student_scores);
Copy after login

Output result:

Array
(
    [80] => Tom
    [90] => Jerry
    [95] => Mickey
)
Copy after login
Copy after login

In this sample code, we use the array_map() function to recombine each key name and corresponding value into one new array. In the callback function, we use [$score, $name] to return a new array containing the corresponding key-value pair. Next, we use the array_combine() function to recombine the new keys and values ​​into a new array $new_student_scores.

Summary

In this article, we introduced how to change the key name and index of an array in PHP. You can use built-in functions such as array_values(), array_combine(), and array_column(), or you can use a foreach loop to iterate through and manually change the keys and indexes of the array. No matter which method you choose, changing the key names and indexes of the array can give you more flexibility and control during development.

The above is the detailed content of How to change the key name or index of a PHP array. For more information, please follow other related articles on the PHP Chinese website!

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