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

How to add field values ​​in php

藏色散人
Release: 2023-03-14 11:50:02
Original
3164 people have browsed it

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"
  }
}
Copy after login

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

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

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!

Related labels:
php
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