Home > Article > Backend Development > Introduction to php built-in function array_column
Array_column is a built-in function of PHP, used to return a specified column in a multi-dimensional array. When using it, please pay attention to the PHP version >= 5.5.0. Lower versions will report an error: "no function".
Function Description
array_column($array, $column1, $column2)
array_column() returns the column whose key is column1 in the array array. If the optional parameter column2 is specified, column2 will also be set as the key of the returned column.
##Example 1
<?php $array = [ ['id' => 100, 'name' => 'test1', 'score' => 1, 'money' => 111], ['id' => 101, 'name' => 'test2', 'score' => 2, 'money' => 222], ['id' => 102, 'name' => 'test3', 'score' => 3, 'money' => 333], ]; $names = array_column($array, 'name'); print_r($names);The results of Example 1 are as follows:
Example 2
<?php $array = [ ['id' => 100, 'name' => 'test1', 'score' => 1, 'money' => 111], ['id' => 101, 'name' => 'test2', 'score' => 2, 'money' => 222], ['id' => 102, 'name' => 'test3', 'score' => 3, 'money' => 333], ]; $names = array_column($array, 'name', 'id'); print_r($names);Example 2 The results are as follows:
##You can see that the column corresponding to the third parameter 'id' in Example 2 has been set as the key of the new array. At the same time, think of the column method of thinkphp database operation for the same reason, as follows:
Generally speaking, array_column is quite commonly used when processing data. It is not used foreach processing is so complicated. But only after a deep understanding can you use it flexibly in the project, so friends, if you try it more, the impression will be more profound!
The above is the detailed introduction of PHP built-in function array_column. For more information, please pay attention to other related articles on PHP Chinese website!
The above is the detailed content of Introduction to php built-in function array_column. For more information, please follow other related articles on the PHP Chinese website!