How to select the specified subscript from an array in php

WBOY
Release: 2023-05-19 10:01:37
Original
605 people have browsed it

In php, we can use the array_column() function to select the key or subscript specified by the array. This function can get a column of the specified array. It requires three parameters: the original array, the key name or subscript of the specified column, and the optional column key or subscript. Let's take a look at the usage of the array_column() function.

  1. Select a column with a specified subscript

The following code demonstrates how to use the array_column() function to select a specified subscript column of an array:

$users = array( array('name' => '张三', 'age' => 28, 'sex' => '男'), array('name' => '李四', 'age' => 29, 'sex' => '女'), array('name' => '王五', 'age' => 30, 'sex' => '男') ); // 选出数组的姓名列 $names = array_column($users, 'name'); print_r($names);
Copy after login

The above code will output the following results:

Array ( [0] => 张三 [1] => 李四 [2] => 王五 )
Copy after login

In the above code, we use the array_column() function to select the 'name' subscript column of the $users array, that is, the 'name' key corresponding to each sub-array value.

  1. Select multiple columns with specified subscripts

You can apply the array_column() function to multiple columns, you only need to include the key name or subscript to be selected in an array That’s it. The following code demonstrates how to select multiple columns of an array:

$users = array( array('name' => '张三', 'age' => 28, 'sex' => '男'), array('name' => '李四', 'age' => 29, 'sex' => '女'), array('name' => '王五', 'age' => 30, 'sex' => '男') ); // 选出数组的姓名和性别列 $list = array_column($users, array('name', 'sex')); print_r($list);
Copy after login

The above code will output the following results:

Array ( [0] => Array ( [name] => 张三 [sex] => 男 ) [1] => Array ( [name] => 李四 [sex] => 女 ) [2] => Array ( [name] => 王五 [sex] => 男 ) )
Copy after login

In the above code, we selected $ using the array_column() function The 'name' and 'sex' subscript columns of the users array, which are contained in an array.

  1. Select the specified subscript columns of multiple arrays

You can use the array_column() function to select the specified subscript columns of multiple arrays. You only need to pass multiple arrays Just give it a function. The following code demonstrates how to select specified subscript columns of multiple arrays:

$users1 = array( array('name' => '张三', 'age' => 28, 'sex' => '男'), array('name' => '李四', 'age' => 29, 'sex' => '女'), array('name' => '王五', 'age' => 30, 'sex' => '男') ); $users2 = array( array('name' => '小明', 'age' => 18, 'sex' => '男'), array('name' => '小红', 'age' => 19, 'sex' => '女'), array('name' => '小刚', 'age' => 20, 'sex' => '男') ); // 选出多个数组的姓名列 $names = array_column(array_merge($users1, $users2), 'name'); print_r($names);
Copy after login

The above code will output the following results:

Array ( [0] => 张三 [1] => 李四 [2] => 王五 [3] => 小明 [4] => 小红 [5] => 小刚 )
Copy after login

In the above code, we use the array_column() function The 'name' subscript column of the $users1 and $users2 arrays is selected, they are first merged into an array, and then the array_column() function is used. If the merged arrays have the same subscript column names, the later ones will gradually overwrite the earlier values.

Summary

The array_column() function is a very practical PHP array function. It can quickly select the specified subscript column or columns of an array, as well as the specified subscripts of multiple arrays. List. In daily PHP development, especially when operating database result sets, the array_column() function can greatly improve the efficiency of the code.

The above is the detailed content of How to select the specified subscript from an array 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!