Home > Backend Development > PHP Tutorial > Introduction to php built-in function array_column

Introduction to php built-in function array_column

幸运切糕
Release: 2020-05-04 10:15:56
Original
215 people have browsed it

Array_column is a built-in function of PHP, used to return a specified column in a multi-dimensional array. When using it, please pay attention to the PHP version >= 5.5.0. Lower versions will report an error: "no function".


Function Description

array_column($array, $column1, $column2)
Copy after login

array_column() returns the column whose key is column1 in the array array. If the optional parameter column2 is specified, column2 will also be set as the key of the returned column.


##Example 1

<?php
$array = [
    [&#39;id&#39; => 100, &#39;name&#39; => &#39;test1&#39;, &#39;score&#39; => 1, &#39;money&#39; => 111],
    [&#39;id&#39; => 101, &#39;name&#39; => &#39;test2&#39;, &#39;score&#39; => 2, &#39;money&#39; => 222],
    [&#39;id&#39; => 102, &#39;name&#39; => &#39;test3&#39;, &#39;score&#39; => 3, &#39;money&#39; => 333],
];
$names = array_column($array, &#39;name&#39;);
print_r($names);
Copy after login

The results of Example 1 are as follows:

Introduction to php built-in function array_column

Example 2

<?php
$array = [
    [&#39;id&#39; => 100, &#39;name&#39; => &#39;test1&#39;, &#39;score&#39; => 1, &#39;money&#39; => 111],
    [&#39;id&#39; => 101, &#39;name&#39; => &#39;test2&#39;, &#39;score&#39; => 2, &#39;money&#39; => 222],
    [&#39;id&#39; => 102, &#39;name&#39; => &#39;test3&#39;, &#39;score&#39; => 3, &#39;money&#39; => 333],
];
$names = array_column($array, &#39;name&#39;, &#39;id&#39;);
print_r($names);
Copy after login
Example 2 The results are as follows:

Introduction to php built-in function array_column

##You can see that the column corresponding to the third parameter 'id' in Example 2 has been set as the key of the new array. At the same time, think of the column method of thinkphp database operation for the same reason, as follows:

Introduction to php built-in function array_column Generally speaking, array_column is quite commonly used when processing data. It is not used foreach processing is so complicated. But only after a deep understanding can you use it flexibly in the project, so friends, if you try it more, the impression will be more profound!

The above is the detailed introduction of PHP built-in function array_column. For more information, please pay attention to other related articles on PHP Chinese website!

The above is the detailed content of Introduction to php built-in function array_column. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template