The PHP array_column method can return a specified column in the array, but cannot return multiple columns, this article describes the use of the Array_column method, and uses the code to demonstrate a method that returns the specified number of columns in the array.
1.array_column description
Array_column can return a column specified in an array
Array Array_column (array $input, mixed $column _key [, mixed $index _key = null])
Parameter description:
input
A multidimensional array that needs to take out the array columns. If you provide an array that contains a set of objects, only the public property is fetched directly. In order for the private and protected properties to be removed, the class must implement the __get () and __isset () magic methods.
Column_key
A column that needs to return a value, which can be a column index of an indexed array, a key for the column of an associative array, or a property name. It can also be null, and this will return the entire array (which works well with the Index_key parameter to reset the array key)
Index_key
As the column that returns the index/key of the array, it can be an integer index of the column, or a string key value.
Example:
Returns the Name column in the array
<?php$arr = Array ( ' id ' =>1001, ' name ' = ' fdipzone ', ' age ' =>18, ' profession ' = ' programmer '), Array (' ID ' =>1002, ' name ' = ' Terry ', ' Age ' =>19, ' profession ' = ' designer '), array (' ID ' =>1003 , ' name ' = ' Alex ', ' age ' =>20, ' profession ' = ' tester '), $result = Array_column ($arr, ' name ');p Rint_r ($result );? >
Output:
Array ( [0] = Fdipzone [1] + Terry [2] = Alex)
2. Returning a method that specifies multiple columns in an array
The Array_column method can return a column specified in an array, but cannot return multiple columns, so this method is written to support the return of more than one column in the array , and the parameter invocation is similar to Array_column.
<?php/** * Returns a multi-column specified in the array * * @param array $input a multidimensional array that requires the array column to be fetched * @param String $column _keys The column name to be fetched, comma-delimited, and return all columns if not passed * @param String $index _key as the column that returns the index of the array * @return array */function array_columns ($input, $column _keys=null, $index _key=nu ll) {$result = array (); $keys =isset ($column _keys)? Explode (', ', $column _keys): Array (); if ($input) {foreach ($input as $k = + $v) {//Specifies the return column if ($keys) {$tmp = array (); foreach ($keys as $key) {$tmp [$key] = $v [$key]; }}else{$tmp = $v; }//Specify the index column if (Isset ($index _key)) {$result [$v [$index _key]] = $tmp; }else{$result [] = $tmp; }}} return $result;} Demo Code $arr = array (' ID ' =>1001, ' name ' = ' fdipzone ', ' age ' =>18, ' profession ' = ' programmer '), array (' id ' =>1002, ' name ' = ' Terry ', ' Age ' =>19, ' Profession ' = ' + ' designer '), array (' ID ' =>1003, ' name ' = ' Alex ', ' age ' =>20, ' profession ' = ' tester '), Echo ' Specify return columns and indexed columns '. php_eol; $result = Array_columns ($arr, ' name,profession ', ' id ');p rint_r ($result); Echo php_eol. ' Specify the return column, not the index column '. php_eol; $result = Array_columns ($arr, ' name,profession ');p rint_r ($result); Echo php_eol. ' Do not specify a return column, specify an index column '. php_eol; $result = array_columns ($arr, NULL, ' ID ');p rint_r ($result); Echo php_eol. ' Do not specify a return column, do not specify an index column '. php_eol; $result = Array_columns ($arr);p rint_r ($result);? >
output:
Specify the return column and index column array ([1001] = = Array ([name] + fdipzone [profession] = Programmer ) [1002] = = Array ([name] = Terry [profession] = designer) [1 003] = = Array ([name] + alex [profession] = Tester)) Specifies the return column, not the index column array ( [0] = = Array ([name] = Fdipzone [profession] = programmer) [1] = = Array ([name] = Terry [profession] = designer) [2] = = Array ( [Name] + alex [profession] = tester)) do not specify a return column, specify an index column array ([1001] = array ([id] = 1001 [Name] = Fdipzone [age] [[profession] = Prog rammer) [1002] = = Array ([id] = 1002 [Name] = Terry [age] = > [profEssion] = designer) [1003] = = Array ([id] = 1003 [name] + Alex [Age] = [profession] = Tester) do not specify a return column, do not specify an index column array ([0] = = Array ( [id] = 1001 [name] + fdipzone [age] [[profession] = Programmer ) [1] = = Array ([id] = 1002 [Name] = Terry [age] + 19 [Profession] = designer) [2] = = Array ([id] = 1003 [name] =&G T Alex [age] [profession] = tester))
This article explains how PHP returns the specified number of columns in the array, and more about the PHP Chinese web.