A very useful PHP array function

Source: Internet
Author: User

Array_column

This function is useful and can be called directly in PHP 5.5.

Have the following two-dimensional array, such as to extract specific items from each sub-array.

<?PHP//Array representing a possible record set returned from a database$records=Array(    Array(        ' id ' = 2135, ' first_name ' and ' John ', ' last_name ' and ' Doe ',    ),Array(        ' id ' = = 3245, ' first_name ' = ' Sally ', ' last_name ' and ' Smith ',    ),Array(        ' id ' = 5342, ' first_name ' = ' Jane ', ' last_name ' and ' Jones ',    ),Array(        ' id ' = 5623, ' first_name ' and ' Peter ', ' last_name ' and ' Doe ',    )); $first _names= Array_column ($records, ' first_name ');Print_r($first _names);?>

return only First_Name

Array (    [0] = John    [1] = Sally    [2] = Jane    [3] = = Peter)

If the third argument is a key name

Array_column ($records, ' last_name ', ' id ');

Return:

Array (    [2135] = Doe    [3245] = Smith    [5342] = Jones    [5623] = > Doe)

A very useful PHP array function

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.