search
HomeBackend DevelopmentPHP ProblemHow to sum specified fields in php two-dimensional array

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)".

How to sum specified fields in php two-dimensional 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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(
  array(
    &#39;name&#39; => "小明",
    &#39;score&#39; => 85,
    &#39;age&#39; => 25
  ),
  array(
    &#39;name&#39; => "小华",
    &#39;score&#39; => 92,
    &#39;age&#39; => 20
  ),
  array(
    &#39;name&#39; => "霄晓",
    &#39;score&#39; => 100,
    &#39;age&#39; => 22
  ),
  array(
    &#39;name&#39; => "萧洁",
    &#39;score&#39; => 99,
    &#39;age&#39; => 21
  ),
  array(
    &#39;name&#39; => "赵峰",
    &#39;score&#39; => 96,
    &#39;age&#39; => 24
  )
);
var_dump($arr);
echo "指定字段 score 的全部元素:";
$score=array_column($arr, &#39;score&#39;);
var_dump($score);

echo "指定字段 age 的全部元素:";
$age=array_column($arr, &#39;age&#39;);
var_dump($age);
?>

How to sum specified fields in php two-dimensional array

Step 2. Use the array_sum() function to sum the result array

echo "指定字段 score 的全部元素:";
$score=array_column($arr, &#39;score&#39;);
var_dump($score);
$sum=array_sum($score);
echo "指定字段 score 的元素和:".$sum;


echo "指定字段 age 的全部元素:";
$age=array_column($arr, &#39;age&#39;);
var_dump($age);
$sum=array_sum($age);
echo "指定字段 age 的元素和:".$sum;

How to sum specified fields in php two-dimensional arrayInstructions:

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft