The Array_column () function in PHP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.