Home >Backend Development >PHP Tutorial >[php learning] array_column() function--efficiently extract multi-dimensional array data

[php learning] array_column() function--efficiently extract multi-dimensional array data

little bottle
little bottleOriginal
2019-04-18 10:02:363876browse

Recently, I heard from a friend that he found several useful functions for processing arrays in PHP. After hearing this, I quickly came to share them with you. This article is about the introduction and use of the array_column function that can efficiently handle multi-dimensional arrays. Friends who are interested should come and learn it.

array_column() function

Function:

Returns the value of a single column in the input array.

Syntax:

array_column(array,column_key,index_key);

Parameters                                                                                                                   . Specifies the multidimensional array (record set) to be used.

column_key

Required. The column whose value needs to be returned.

can be an integer index of a column of an index array, or a string key value of a column of an associative array.

This parameter can also be NULL, in which case the entire array will be returned (very useful when used with the index_key parameter to reset the array key).

index_key

Optional. The column used as the index/key of the returned array.

Scenario example:

1. Usually we get the array result set by querying the database. When we need to format the array with the primary key id as key, there is no need to loop. Reorganize the array and call the function directly to return. Example:

*

Note: Use a unique value as the key

, otherwise data overwriting will occur

Look at the output results:

2. Extract the data value of a column in the result set

Look at the output:


##Summary:

Use The array_column function can easily extract array data without having to write loop statements to traverse the extraction. Note that the array is: use a multi-dimensional array.

If you don’t want to take any detours, please pay attention to the PHP Chinese website. There are more PHP video tutorials

waiting for you to learn!

The above is the detailed content of [php learning] array_column() function--efficiently extract multi-dimensional array data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more