Home  >  Article  >  Backend Development  >  php remove keys from array

php remove keys from array

王林
王林Original
2023-05-23 12:20:37422browse

In PHP development, using arrays is a very common operation. Arrays can store different types of data and can be accessed and manipulated through key-value pairs. However, sometimes we need to remove the keys of the array and only keep the value part of the array. This article will introduce how to remove the keys of an array in PHP.

1. Use the array_values() function

PHP provides a built-in function array_values(). This function will return a new array, and the value of the array is the value part of the original array. And will be re-indexed in order. We can use this function in the following way:

$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$newArray = array_values($array);
print_r($newArray);

Output:

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

2. Using foreach loop

We can also use foreach loop to traverse the array and remove the array key. The specific implementation is as follows:

$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$newArray = array();
foreach ($array as $value) {
    $newArray[] = $value;
}
print_r($newArray);

Output:

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

3. Use the array_map() function

Similarly, PHP also provides the array_map() function to remove the array key function. array_map() applies a callback function to each element of the array and returns a new array. We can use this function in the following way:

$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$newArray = array_map(function ($value) {
    return $value;
}, $array);
print_r($newArray);

Output:

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

The output result in the above example still contains the key value, because the array_map() function uses the key of the original array Value pairs are operated on. In fact, we can pass in an anonymous callback function to just return the value of the array. As shown below:

$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$newArray = array_map(function ($value) {
    return $value;
}, array_values($array));
print_r($newArray);

Output results:

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

Summary

In this article, we introduced three common methods to remove key values ​​​​from PHP arrays. Using the array_values() function is the simplest, you only need to call the function; using the foreach loop requires creating a new array and implementing it through the loop; using the array_map() function, you need to pass in an anonymous function to operate. These methods allow us to operate arrays more flexibly and choose the most suitable method according to actual needs.

The above is the detailed content of php remove keys from 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