Summing steps: 1. Use the array_column() function to obtain all elements of the specified field (column) in the two-dimensional array. The syntax "array_column(two-dimensional array, 'specified field name')" will return a A result array containing all elements of the specified field; 2. Use the array_sum() function to calculate the sum of all elements in the result array, with the syntax "array_sum (result array)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the array_column() function and array_sum() function to obtain all elements of the specified field in the two-dimensional array and sum them.
Implementation steps:
Step 1: Use the array_column() function to obtain all elements of the specified field (column) in the two-dimensional array
array_column() Returns the value of a single column in the input array.
array_column(array,column_key,index_key);
Parameters | Description |
---|---|
array | Required. Specifies the multidimensional array (record set) to use. |
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 that is the index/key of the returned array. |
Return value: Return an array. The value of the array is the value of a single column in the input array.
Just set the second parameter of the function to the specified field name to get all the elements of the specified field (column) in the two-dimensional array
<?php header('content-type:text/html;charset=utf-8'); $arr=array( array( 'name' => "小明", 'score' => 85, 'age' => 25 ), array( 'name' => "小华", 'score' => 92, 'age' => 20 ), array( 'name' => "霄晓", 'score' => 100, 'age' => 22 ), array( 'name' => "萧洁", 'score' => 99, 'age' => 21 ), array( 'name' => "赵峰", 'score' => 96, 'age' => 24 ) ); var_dump($arr); echo "指定字段 score 的全部元素:"; $score=array_column($arr, 'score'); var_dump($score); echo "指定字段 age 的全部元素:"; $age=array_column($arr, 'age'); var_dump($age); ?>
Step 2. Use the array_sum() function to sum the result array
echo "指定字段 score 的全部元素:"; $score=array_column($arr, 'score'); var_dump($score); $sum=array_sum($score); echo "指定字段 score 的元素和:".$sum; echo "指定字段 age 的全部元素:"; $age=array_column($arr, 'age'); var_dump($age); $sum=array_sum($age); echo "指定字段 age 的元素和:".$sum;
Instructions:
array_sum() function can Calculates the sum of all elements in the specified array.
array_sum ( $arr )
If all elements of array arr are integers, return an integer value; if one or more of the values are floating point numbers, return a floating point number.
If there are non-numeric elements in the array arr, PHP will try to convert them to a numeric value, and if the conversion fails, it will be treated as a 0 value. For example, the string "45" will be converted to the integer 45, and the string "12.4abc" will be converted to the decimal 12.4.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to sum specified fields in php two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

求和方法:1、用array_column()获取多维数组中指定一列的全部元素,语法“rray_column(数组, '指定列名')”,会返回一个包含全部元素的结果数组;2、用“array_sum(结果数组)”计算结果数组中所有元素的和即可。

增加元素的方法:1、使用array_push()函数,语法“array_push(二维数组,值1,值2...);”;2、使用array_splice()函数,语法“array_splice(二维数组,count(二维数组),0,元素值)”。

获取方法:1、用“array_values(数组)”将指定数组转为索引数组;2、用“array_search(数值,索引数组)”,在索引数组中搜索数值,返回对应的索引值(下标);3、用“索引值+1”语句获取元素在数组中的位置值。

方法:1、循环遍历数组,语法“foreach($arr as $k=>$v){}”;2、循环体中,用“==”判断元素值是否为null,如果是则用unset()删除该元素,语法“if($v==null){unset($arr[$k]);}”。

php数组里面可以放数组。PHP数组可以存储所有类型的数据,当然也包括数组本身;如果一个数组中的元素是另一个数组,就构成了包含数组的数组,即多维度数组。数组的不同维度标志着需用几个下标(索引)来获取对应的数组元素,比如二维数组需用两个下标。

php中只比较值的数组交集函数是“array_intersect()”;该函数用于比较两个(或更多个)数组的键值,语法“array_intersect(数组1,数组2...)”,会返回一个交集数组,所包含的值是从被比较的数组(数组1)中取。

去掉方法:1、用“sort($arr)”对数组进行升序排序,排序后该数组的第一个元素就是最小值,最后一个元素就是最大值;2、用“array_pop($arr)”删除最后一个元素,用“array_shift($arr)”删除第一个元素即可。

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)