PHP removes the key name from the array

WBOY
Release: 2023-05-19 16:01:37
Original
779 people have browsed it

In PHP, array is a very common data type that can be used to store a series of related data. In some cases, we may need to remove the key names of the array and keep only the key values. This article introduces several methods to achieve this function.

Method 1: Use the array_values ​​function

The array_values ​​function can return all the values ​​in an array and re-index the key name of the array. If the keys in the original array are numbers, the returned array will be numbered consecutively.

For example, there is an associative array:

$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
Copy after login
Copy after login

We can use the array_values ​​function to remove the key name:

$newFruits = array_values($fruits);
Copy after login

At this time, the value of $newFruits is:

Array (
    [0] => Apple
    [1] => Banana
    [2] => Cherry
)
Copy after login
Copy after login
Copy after login

You can see that the key names in the new array have been removed, leaving only the key values.

Method 2: Use foreach loop

Another method is through foreach loop. The specific steps are as follows:

  1. Create a new array
  2. Use a foreach loop to traverse the original array and store each value in the original array into the new array

For example, there is an associative array:

$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
Copy after login
Copy after login

We can use a foreach loop to remove the key name:

$newFruits = array();
foreach ($fruits as $value) {
    array_push($newFruits, $value);
}
Copy after login

At this time, the value of $newFruits is also:

Array (
    [0] => Apple
    [1] => Banana
    [2] => Cherry
)
Copy after login
Copy after login
Copy after login

As you can see, by traversing the original array through a foreach loop and storing each value in the original array into a new array, the key name can be removed.

Method 3: Use the array_map function

Finally, we can also use the array_map function to remove the array key name.

$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
$newFruits = array_map(function($value) {
    return $value;
}, $fruits);
Copy after login

Here, we pass an anonymous function as the first parameter of array_map. The purpose of this function is to return the value in the original array.

The running result is also:

Array (
    [0] => Apple
    [1] => Banana
    [2] => Cherry
)
Copy after login
Copy after login
Copy after login

It can be found that by using the array_map function, we can also remove the key name of the array.

Summary

In PHP, to remove the key name of an array, we can use a variety of methods, such as array_values ​​function, foreach loop and array_map function. Which method to choose depends on personal habits and actual needs.

The above is the detailed content of PHP removes the key name from the 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