Home  >  Article  >  Backend Development  >  How to modify the subscript of a php array

How to modify the subscript of a php array

PHPz
PHPzOriginal
2023-04-27 09:08:501047browse

In PHP, array is a very common and widely used data type. During the development process, sometimes we need to modify the subscript of the array to meet different needs.

There are many ways to modify the array subscript. Below I will introduce several common methods.

1. Use array_combine() function

array_combine() function can combine two arrays into a new array, where the value of the first array is the key of the new array, and the value of the second array is the key of the new array. The value of the array is the value of the new array.

You can use the following code to change the array subscript from the original numeric form to a numeric form starting from 1 or other specific string form:

$old_arr = array('a', 'b', 'c');
$new_arr = array_combine(range(1, count($old_arr)), $old_arr);

The range() function here uses To generate an array of numbers from 1 to the length of the original array. The running result is:

Array
(
    [1] => a
    [2] => b
    [3] => c
)

2. Use the array_values() function

array_values() function to return all the values ​​of an array and re-index the keys. This function can be used to modify array subscripts.

You can use the following code to re-assign the index to the array starting from 0 and the subscript of the number increasing by 1:

$old_arr = array('a', 'b', 'c');
$new_arr = array_values($old_arr);

The running result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

3. Use foreach loop

Using the foreach loop, you can traverse the array and modify the subscript of each element, or you can use a reference to modify the value of each element.

The following code demonstrates how to modify the subscript of the array:

$old_arr = array('one' => 1, 'two' => 2, 'three' => 3);
$new_arr = array();
foreach ($old_arr as $key => $value) {
    $new_arr[$key . '_new'] = $value;
}

The running result is:

Array
(
    [one_new] => 1
    [two_new] => 2
    [three_new] => 3
)

4. Use the array_map() function

Use The array_map() function applies a callback function to each element of the array and returns a new array containing the elements processed by the callback function.

You can use the following code to change the array subscript to lowercase:

$old_arr = array('APPLE' => 'red', 'ORANGE' => 'orange', 'BANANA' => 'yellow');
$new_arr = array_map('strtolower', array_flip($old_arr));

The array_flip() function here is used to exchange the keys and values ​​of the array. The running result is:

Array
(
    [red] => apple
    [orange] => orange
    [yellow] => banana
)

Summary

This article introduces several common methods of modifying PHP array subscripts, including using array_combine(), array_values(), foreach loop and array_map() functions.

In actual development, you can choose different methods to operate according to your needs. No matter which method is used, you need to pay attention to whether the modified array meets the original requirements.

The above is the detailed content of How to modify the subscript of a php array. 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