PHP Gets an example of how to get a single column value in an array

Source: Internet
Author: User
This article mainly introduces the method that PHP obtains the single-column value in the array, and analyzes the use technique of the array_column () function in PHP5.5 in combination with the instance form, the friend who needs can refer to the following

This example describes how PHP obtains a single-column value in an array. Share to everyone for your reference, as follows:

The values for the single column in the Get array in PHP are as follows:

Use the array function in PHP array_column() : Returns the value of a single column in the array. (PHP 5.5+ applies)

Grammar:

array_column(array,column_key,index_key);

Parameters:

array : required, specified must be a multidimensional array;
Column_key : Required, the key name of the value to be returned, either an integer index of the column of the indexed array, or a string key value for the column of the associated array. This parameter can also be NULL, which is useful when the entire array is returned (with the Index_key parameter to reset the array key).
index_key : Optional. The column that is used as the index/key of the returned array.

Instance:

Remove the Last_Name column from the recordset and use the corresponding "id" column as the key value:

<?php//represents the array of possible recordsets returned by the database $ A = array (' id ' = ' = ' = '  5698,  ' first_name ' = ' Bill ', '  last_name ' = > ' Gates ',), array ('  id ' = = 4767,  ' first_name ' = ' Steve ',  ' last_name ' = ' Jobs ',) array (  ' id ' = ' = 3809, ' first_name ' = ' Mark ', ' last_name ' = '  Zuckerberg ', '); $last _names = Array_column ($a, ' Last_Name ', ' id ');p rint_r ($last _names);? >

Output:

Array ([5698] = Gates [4767] = Jobs [3809] = Zuckerberg)

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.