Home >Backend Development >PHP Problem >How to specify column sum in php array

How to specify column sum in php array

PHPz
PHPzOriginal
2023-04-25 10:43:11560browse

In PHP programming, we often deal with array contents. When working with arrays, we sometimes need to sum certain columns in the array. In this article, I will share how to sum specified columns in PHP.

First, let’s look at the following array example:

$data = array(
    array('name' => 'John', 'age' => 25, 'salary' => 5000),
    array('name' => 'Sarah', 'age' => 30, 'salary' => 6000),
    array('name' => 'Jason', 'age' => 35, 'salary' => 7000),
    array('name' => 'Linda', 'age' => 40, 'salary' => 8000),
    array('name' => 'David', 'age' => 45, 'salary' => 9000),
);

In this array example, we have five elements, each element contains name, age and salary three keys and values. Now, we want to sum the salary columns of this array.

In PHP, we can use the array_column function to get a specified column in an array, and then use the array_sum function to sum the column. The following is the sample code:

$salaries = array_column($data, 'salary');
$totalSalary = array_sum($salaries);

echo 'The total salary is: ' . $totalSalary;

In this sample code, the array_column function is used to get the salary column in the $data array. This creates a new array called $salaries that contains the salaries of all employees. We can then use the array_sum function to sum all the elements in the $salaries array. This creates a total salary variable named $totalSalary. Finally, we output the summation result through the echo statement.

Now, let’s see how to sum multiple columns simultaneously in an array. If we want to sum both the age and salary columns, we can use the following code:

$ages = array_column($data, 'age');
$totalAge = array_sum($ages);

$salaries = array_column($data, 'salary');
$totalSalary = array_sum($salaries);

echo 'The total age is: ' . $totalAge;
echo 'The total salary is: ' . $totalSalary;

In this example code, we create two new The arrays $ages and $salaries are used to obtain the values ​​of the age and salary columns respectively. We then use the array_sum function to sum all the elements in these two arrays. Finally, we output the summation result through the echo statement.

To summarize, summing specified columns in PHP is very simple. We only need to use two functions: array_column and array_sum. If we need to sum multiple columns, we can use these two functions multiple times and create new arrays separately. Using these functions can help us process array data in PHP quickly and effectively.

The above is the detailed content of How to specify column sum in php 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