Home >Backend Development >PHP Problem >How to add field values ​​in php

How to add field values ​​in php

藏色散人
藏色散人Original
2021-12-10 09:09:403161browse

php method to add field values: 1. Use the array_column() function to return the value of a single column in the input array; 2. Use the array_sum() function to return the sum of the values ​​in the array. .

How to add field values ​​in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to add field values ​​in php?

Add a field value in a PHP array (summation)

Use the array function: array_column() to return the value of a single column in the input array

Use the array sum function: array_sum() to return the sum of the values ​​in the array

array(6) {
  [0]=>
  array(1) {
    ["pv"]=>
    string(4) "1.72"
  }
  [1]=>
  array(1) {
    ["pv"]=>
    string(4) "1.88"
  }
  [2]=>
  array(1) {
    ["pv"]=>
    string(5) "22.90"
  }
  [3]=>
  array(1) {
    ["pv"]=>
    string(4) "2.29"
  }
  [4]=>
  array(1) {
    ["pv"]=>
    string(4) "2.04"
  }
  [5]=>
  array(1) {
    ["pv"]=>
    string(4) "1.81"
  }
}

Find the sum of the pv values ​​in the array

$res = array_sum(array_column($att,'pv'));

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to add field values ​​in php. 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