The Array_column () function in PHP
PHP Array Reference Manual
To remove the Last_Name column from the recordset:
<?php//may return the array from the database $ A = array (' id ' = ' = ' 5698, ' first_name ' = ' Peter ', ' last_name ' = > ' Griffin ', ), array ( ' id ' = = 4767, ' first_name ' = ' Ben ', ' last_name ' = ' Smith ', ), array ( ' id ' = = 3809, ' first_name ' = ' Joe ', ' last_name ' = ' Doe ') ; $last _names = Array_column ($a, ' last_name ');p rint_r ($last _names);? >
Output:
Array ( [0] = Griffin [1] = Smith [2] = Doe)
Definition and usage
Array_column () returns the value of a single column in the input array.
Grammar
Array_column (Array,column_key,index_key);
Parameters
Describe
Array required. Specifies the multidimensional array (recordset) to use.
Column_key required. A column that requires a return value. Can be an integer index of the column of an indexed array, or a string key value for a column of an associative array. This parameter can also be NULL, which will return the entire array (which works well with the Index_key parameter to reset the array key).
Index_key is optional. As the column that returns the index/key of the array.
Technical details
return value:
Returns an array whose values are the values of a single column in the input array.
Remove the Last_Name column from the recordset and use the corresponding "id" column as the key value:
<?php//may return the array from the database $ A = array (' id ' = ' = ' 5698, ' first_name ' = ' Peter ', ' last_name ' = > ' Griffin ', ), array ( ' id ' = = 4767, ' first_name ' = ' Ben ', ' last_name ' = ' Smith ', ), array ( ' id ' = = 3809, ' first_name ' = ' Joe ', ' last_name ' = ' Doe ') ; $last _names = Array_column ($a, ' last_name ', ' id ');p rint_r ($last _names);? >
Output:
Array ( [5698] = Griffin [4767] = Smith [3809] = Doe)
Full PHP Array Reference manual