Home >Backend Development >PHP Problem >How to remove array and reset subscript in php
In PHP, we often need to perform some processing on arrays. One of the common tasks in processing is "removing arrays and resetting subscripts". This article will delve into how to achieve this task using PHP.
First, let’s see how to remove elements from an array. In PHP, we can use the unset() function to delete values in an array, as shown below:
$arr = array(1, 2, 3, 4, 5); unset($arr[2]);
In the above example, we deleted the 3rd element in the array. After executing this command, the array becomes:
[0 => 1, 1 => 2, 3 => 4, 4 => 5]
As you can see, element 3 has been deleted, but the index of the array has not been reset.
Next, let’s take a look at how to reset the index. In PHP, there are two ways to achieve this:
Method 1: array_values()
Using the array_values() function will return a new array, and the index of this new array will be from 0 starts. The following is a sample code:
$arr = array(1, 2, 3, 4, 5); unset($arr[2]); $arr = array_values($arr);
After executing the above code, the value of $arr will be:
[0 => 1, 1 => 2, 2 => 4, 3 => 5]
Method 2: Loop traversal
Another method is through Loop through the array and execute the unset() function. This approach does not create a new array and therefore runs more efficiently. The following is the sample code:
$arr = array(1, 2, 3, 4, 5); unset($arr[2]); foreach ($arr as $key => $value) { $arr[$key] = $value; }
After executing the above code, the value of $arr will be:
[0 => 1, 1 => 2, 2 => 4, 3 => 5]
The foreach loop here traverses the array and resets the array using the $key variable The index of each element.
Now we will combine these two steps so that we get a complete PHP program that can efficiently remove elements of an array and reset it index. The following is the complete sample code:
function delete_and_reset(&$arr, $delete_key) { unset($arr[$delete_key]); foreach ($arr as $key => $value) { $arr[$key] = $value; } return $arr; } $arr = array(1, 2, 3, 4, 5); $arr = delete_and_reset($arr, 2);
In the above code, we define a function named delete_and_reset(). It accepts two parameters: the array to operate on and the key to delete.
In the function, we first use the unset() function to delete the element with the specified key, then use foreach() to loop through the array and use the $key variable to reset the index of each element.
Finally, we define an array in the main program and call the delete_and_reset() function to process it. After executing the above code, the value of $arr will be:
[0 => 1, 1 => 2, 2 => 4, 3 => 5]
Now you know how to strip an array and reset the subscripts using PHP. This is a very useful task that will be used frequently in your PHP development work. Whether you are a beginner or a professional developer, mastering this skill is a must.
The above is the detailed content of How to remove array and reset subscript in php. For more information, please follow other related articles on the PHP Chinese website!