How to loop through array keys in php

PHPz
Release: 2023-04-19 13:54:26
Original
504 people have browsed it

In PHP, sometimes we need to remove the keys of all elements in an array and only keep their values. This operation usually needs to be performed in a loop, so we need to know how to use a loop to remove keys from an array. In this article, we will introduce some methods to achieve this operation, so that you can easily deal with arrays.

Method 1: Use a foreach loop

The most basic method is to use a foreach loop to traverse the array and store the value of each element into a new array (that is, remove the key). Here is a demo code:

// 原始数组 $data = array('a' => 1, 'b' => 2, 'c' => 3); // 新数组 $new_data = array(); // foreach循环 foreach($data as $value) { // 将元素的值存储到新数组中 $new_data[] = $value; } // 输出新数组 print_r($new_data);
Copy after login

This will output the following results:

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

Method 2: Using array_values function

PHP provides a function called array_values, which can Stores the values of all elements of an array into a new array, representing them with consecutive numeric keys. This function is very suitable for our needs. Here is a demo code:

// 原始数组 $data = array('a' => 1, 'b' => 2, 'c' => 3); // 新数组 $new_data = array_values($data); // 输出新数组 print_r($new_data);
Copy after login

This will output the following results:

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

Method 3: Using the array_map function

PHP also provides a function called array_map, which Each element in an array can be transformed and the result stored in a new array. We can use this function to remove keys from an array. Here is a demo code:

// 原始数组 $data = array('a' => 1, 'b' => 2, 'c' => 3); // array_map函数 $new_data = array_map(function($value) { return $value; }, $data); // 输出新数组 print_r($new_data);
Copy after login

This will output the following results:

Array ( [a] => 1 [b] => 2 [c] => 3 )
Copy after login

The array_map function of the above code just returns the value of each element itself. This can be achieved by using an anonymous function and passing it as the first argument to the array_map function. The second parameter of the array_map function is the original array. This will call the anonymous function once for each element in the original array and store the result in the new array.

Method 4: Use the array_column function

PHP’s array_column function can obtain the value of the specified column from a multi-dimensional array. In a one-dimensional array, this function can be used to remove the keys and return all values. Here is a demo code:

// 原始数组 $data = array('a' => 1, 'b' => 2, 'c' => 3); // array_column函数 $new_data = array_column(array($data), 0); // 输出新数组 print_r($new_data);
Copy after login

This will output the following results:

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

In the above code, we first wrap the original array into a multi-dimensional array, because the array_column function requires a multi-dimensional array as input . Then, we specify 0 as the column index to represent the value column in the array. This will return a new array consisting of all values.

Conclusion

In PHP, we have many ways to remove the keys of an array and keep only their values. We can use foreach loop, array_values function, array_map function or array_column function to achieve this operation. Which method to choose depends on the specific situation, i.e. input data type, coding style preference, etc. Hopefully the techniques described in this article will help you accomplish what you want to achieve.

The above is the detailed content of How to loop through 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
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!