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);
Output result:
Array ( [0] => apple [1] => banana [2] => cherry )
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);
Output result:
Array ( [1] => apple [2] => banana [3] => cherry )
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);
Output result:
Array ( [1] => apple [2] => banana [3] => cherry )
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);
Output result:
Array ( [80] => Tom [90] => Jerry [95] => Mickey )
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);
Output result:
Array ( [80] => Tom [90] => Jerry [95] => Mickey )
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!

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
