How to remove array keys in PHP

PHPz
Release: 2023-04-24 15:02:41
Original
670 people have browsed it

In PHP, array is a very commonly used data structure. Arrays can use strings or numbers as keys to store multiple values. Usually, when performing array operations, we need to perform operations such as sorting, deduplication, and filtering of the array. In some cases, we need to remove keys from the array (i.e. renumber the keys) and make the array index start from 0. This article explains how to remove array keys in PHP.

1. Use the array_values ​​function

PHP provides a function called array_values, which can re-index all the values ​​in an array and return a new array. Using this function, you can easily remove array keys.

Sample code:

<?php
$arr = array(&#39;a&#39;=>'apple', 'b'=>'banana', 'c'=>'cherry');
$new_arr = array_values($arr);
print_r($new_arr);
?>
Copy after login

Output result:

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

In this example, the original array contains 3 elements and 3 keys. Pass this array to the array_values ​​function and the resulting new array contains only 3 elements and its keys have been renumbered, starting from 0. Using this method, array keys can be easily removed without having to modify the original array.

2. Use foreach to traverse

In addition to using the array_values ​​function, we can also use the foreach statement to traverse the original array and insert elements into the new array.

Sample code:

<?php
$arr = array(&#39;a&#39;=>'apple', 'b'=>'banana', 'c'=>'cherry');
$new_arr = array();
foreach($arr as $value) {
    $new_arr[] = $value;
}
print_r($new_arr);
?>
Copy after login

Output result:

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

In this example, we use the foreach statement to iterate over the original array. By looping through the array, insert each element from the original array into the new array. Finally, the resulting new array has the original array's keys removed and elements can be indexed starting from 0.

3. Use the array_map function

In addition to using array_values ​​and foreach statements, we can also use the built-in function array_map in PHP. This function applies a function to one or more arrays and returns a new array containing the results returned by the function.

Sample code:

<?php
$arr = array(&#39;a&#39;=>'apple', 'b'=>'banana', 'c'=>'cherry');
$new_arr = array_map(function($value) { return $value; }, $arr);
print_r($new_arr);
?>
Copy after login

Output result:

Array ( [a] => apple [b] => banana [c] => cherry )
Copy after login

In this example, we used the array_map function to apply an anonymous function. Since the anonymous function only returns the element itself, the elements in the new array are the same as those in the original array.

Although using the array_map function to remove array keys may seem a bit verbose, it is flexible and applicable. For example, we can use this function to convert each value in an array to uppercase letters, stripping the array keys in the process:

Sample code:

<?php
$arr = array(&#39;a&#39;=>'apple', 'b'=>'banana', 'c'=>'cherry');
$new_arr = array_map(function($value) { return strtoupper($value); }, $arr);
print_r($new_arr);
?>
Copy after login

Output results:

Array ( [a] => APPLE [b] => BANANA [c] => CHERRY )
Copy after login

By using the array_map function, you can quickly and flexibly operate on the array without modifying the original array.

4. Use the reset and prev functions

PHP provides the reset and prev functions, which can loop through the array and return its first or previous element. Using these two functions, you can retrieve elements from an array in order and insert them into a new array.

Sample code:

<?php
$arr = array(&#39;a&#39;=>'apple', 'b'=>'banana', 'c'=>'cherry');
$new_arr = array();
while (list(, $value) = each($arr)) {
    $new_arr[] = $value;
}
print_r($new_arr);
?>
Copy after login

Output result:

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

In this example, we use a while loop to iterate over the original array and retrieve each of its elements using the each function. In the loop, we insert each element into the new array. Finally, the resulting new array has the array keys removed.

Although using the reset and prev functions may not be as fast as other methods, they are flexible and applicable and can be used for other array operations.

Conclusion

Removing array keys in PHP is a common task as it makes arrays more compact and easier to manipulate. In this article, we introduced four methods to remove array keys: using the array_values ​​function, using foreach traversal, using the array_map function, and using the reset and prev functions. Through these methods, arrays can be flexibly manipulated to meet various needs. No matter what kind of operation you do with the array, it is important to protect the original array and use the new array to perform the operation as shown in this article.

The above is the detailed content of How to remove array keys in PHP. 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