php remove keys from array

王林
Release: 2023-05-23 12:20:37
Original
463 people have browsed it

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);
Copy after login

Output:

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

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);
Copy after login

Output:

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

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);
Copy after login

Output:

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

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);
Copy after login

Output results:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!