php remove keys from array

WBOY
Release: 2023-05-07 17:10:08
Original
450 people have browsed it

PHP is a web programming language. The language is known for being simple, easy to learn, efficient, and powerful. It is also very versatile and can be used to develop websites, web applications, desktop applications, command line tools and many other applications. Among them, array is one of the most commonly used data types in PHP, but a common array usually contains two parts: key and value. But occasionally we need to remove the keys in the array and keep only the values, so how should we do it? The following will introduce you to how to remove keys from an array using PHP.

Use the array_values() function

PHP provides a built-in function array_values(), which can easily remove the keys in the array and keep only the values. This function returns a new array containing only the values ​​in the original array. Using this function is not only convenient, but also efficient.

The syntax is as follows:

array array_values ​​(array $array)

Among them, the $array parameter is the array that needs to be processed. This function returns the new array with the keys removed.

Example:

We can demonstrate the use of the array_values() function through an example. Suppose there is an associative array that contains information such as students' names and ages. As shown below:

$students = array(
    "Tom" => 18,
    "Jerry" => 19,
    "Peter" => 20,
    "David" => 18
);
Copy after login

Now, we need to remove the keys from this array and keep only the values. You can use the following code:

$new_array = array_values($students);
print_r($new_array);
Copy after login

The result output is as follows:

Array
(
    [0] => 18
    [1] => 19
    [2] => 20
    [3] => 18
)
Copy after login

You can see that the new array only contains the values ​​​​in the original array, and the order is consistent with the original array.

Use loop traversal method

In addition to using built-in functions, we can also remove keys in the array through loop traversal. The implementation process of this method is relatively complicated, but it can control the array traversal process more flexibly.

Example:

We can use the following code to remove the keys in the array using loop traversal:

$students = array(
    "Tom" => 18,
    "Jerry" => 19,
    "Peter" => 20,
    "David" => 18
);

$new_array = array();
foreach($students as $value){
    $new_array[] = $value;
}
print_r($new_array);
Copy after login

This code adds the values ​​​​in the original array one by one to In the new array, the key is removed. The running results are the same as using the array_values() function. If you need more control over the traversal process, such as specifying the order of traversal, filtering out certain elements, etc., you can add more control conditions to the foreach statement.

Summary

PHP provides a variety of methods to remove the keys in the array and retain only the values. This can be achieved quickly using the built-in function array_values(). Using loop traversal can control the array traversal process more flexibly. When using it, you need to choose the appropriate method according to specific 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