


In PHP, it is often necessary to process and operate arrays. Sometimes, you need to obtain a specific column of data from a multi-dimensional array. In this case, you can use the array_column() function to achieve this. This article will introduce the usage of array_column() function in detail.
1. What is the array_column() function
array_column() is a PHP function used to obtain a specified column of data from a multi-dimensional array. This function was introduced in PHP 5.5 and supports associative arrays and indexed arrays.
Function prototype:
array_column ( array $array , mixed $column_key [, mixed $index_key = null ] ) : array
Parameter description:
$array: to be operated Multidimensional array;
$column_key: the key name or value of the column to be obtained;
$index_key: optional parameter, if this value is specified, it will be used as the key name of the returned array.
2. Usage Examples
Let’s look at some usage examples of the array_column() function.
- Index array
Suppose we have an index array $users, which stores some user information, and we want to get the names of all users from it. This can be achieved through the following code:
$users = array(0 => array('id'=>1, 'name'=>'Tom', 'email'=>'tom@example.com'), 1 => array('id'=>2, 'name'=>'Jerry', 'email'=>'jerry@example.com'), 2 => array('id'=>3, 'name'=>'Mike', 'email'=>'mike@example.com')); $names = array_column($users, 'name'); print_r($names);
The output result is:
Array ( [0] => Tom [1] => Jerry [2] => Mike )
- Associative array
If our array is an associative array, we can Get a specified column of data in the same way.
For example, we have an associative array $students, which contains some information about students. We want to get the ages of all students:
$students = array('Tom' => array('age'=>20, 'gender'=>'male', 'city'=>'Beijing'), 'Jerry' => array('age'=>21, 'gender'=>'female', 'city'=>'Shanghai'), 'Mike' => array('age'=>19, 'gender'=>'male', 'city'=>'Guangzhou')); $ages = array_column($students, 'age'); print_r($ages);
The output result is:
Array ( [Tom] => 20 [Jerry] => 21 [Mike] => 19 )
- Get multiple key values
If we want to get multiple key values from a multi-dimensional array, we can put the key names to be obtained in an array and pass them as the second parameter Enter the array_column() function.
For example, we have an array $temperatures that contains temperature data for multiple cities. We want to get the average temperature and maximum temperature of each city. We can do this:
$temperatures = array( array('city'=>'Beijing', 'average'=>20, 'highest'=>28), array('city'=>'Shanghai', 'average'=>23, 'highest'=>30), array('city'=>'Guangzhou', 'average'=>25, 'highest'=>32) ); $infos = array_column($temperatures, array('average', 'highest'), 'city'); print_r($infos);
Output results Specify the key name of the returned array for:
Array ( [Beijing] => Array ( [average] => 20 [highest] => 28 ) [Shanghai] => Array ( [average] => 23 [highest] => 30 ) [Guangzhou] => Array ( [average] => 25 [highest] => 32 ) )
Sometimes, we want to specify the key name of the returned array when obtaining the data of the specified column. Can be specified in the third parameter of the array_column() function.
For example, in the previous example, we hope to use "average temperature" and "maximum temperature" as the key names of the returned array. We can write like this:
$temperatures = array( array('city'=>'Beijing', 'average'=>20, 'highest'=>28), array('city'=>'Shanghai', 'average'=>23, 'highest'=>30), array('city'=>'Guangzhou', 'average'=>25, 'highest'=>32) ); $infos = array_column($temperatures, array('average', 'highest'), 'city'); $infos = array_map(function($v){return ['平均气温'=>$v[0], '最高气温'=>$v[1]];}, $infos); print_r($infos);
The output result is:
Array ( [Beijing] => Array ( [平均气温] => 20 [最高气温] => 28 ) [Shanghai] => Array ( [平均气温] => 23 [最高气温] => 30 ) [Guangzhou] => Array ( [平均气温] => 25 [最高气温] => 32 ) )
3. Conclusion
The array_column() function can easily obtain a specified column of data from a multi-dimensional array, and the method of use is simple and clear. In actual development, we can make full use of this function to improve the efficiency and accuracy of array processing.
The above is the detailed content of Detailed explanation of the usage of PHP's array_column() function. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.