Home  >  Article  >  Backend Development  >  What function is used to specify the sum of a column in a php array?

What function is used to specify the sum of a column in a php array?

青灯夜游
青灯夜游Original
2023-01-07 17:42:293660browse

To specify the sum of a column in a php array, use the array_column() and array_sum() functions. Steps: 1. Use array_column() to obtain all elements of a specified column in the array. The syntax "array_column(array, 'specified column name')" will return a result array containing all elements; 2. Use array_sum() function to calculate the result. The sum of all elements in the array is enough, the syntax is "array_sum (result array)".

What function is used to specify the sum of a column in a php array?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Use array_column to specify a column in the php array to sum. () and array_sum() functions.

  • array_column() function can obtain all elements of a specified column in the array

  • array_sum() function can calculate all elements in the array and

Implementation steps:

Step 1: Use the array_column() function to specify The value of a specified column in the array

array_column() can return the value of a single column in the specified array; it will return an array, and the array value is the value of the specified column.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$user = array(
 &#39;0&#39; => array(&#39;id&#39; => 100, &#39;username&#39; => &#39;a1&#39;),
 &#39;1&#39; => array(&#39;id&#39; => 101, &#39;username&#39; => &#39;a2&#39;),
 &#39;2&#39; => array(&#39;id&#39; => 102, &#39;username&#39; => &#39;a3&#39;),
 &#39;3&#39; => array(&#39;id&#39; => 103, &#39;username&#39; => &#39;a4&#39;),
 &#39;4&#39; => array(&#39;id&#39; => 104, &#39;username&#39; => &#39;a5&#39;),
);
var_dump($user);
$id=array_column($user, &#39;id&#39;);
var_dump($id);
?>

What function is used to specify the sum of a column in a php array?

Step 2: Use array_sum() to sum the result array

array_sum() function can calculate all the results in the specified array Sum of elements

$sum=array_sum($id);
echo "多维数组中id列的和:".array_sum($id);

What function is used to specify the sum of a column in a php array?

Extended knowledge: array_product() function

array_sum() function can be calculated The sum of array elements, while the array_product() function can calculate the product of array elements.

<?php
$array= array(2,3,4);
echo &#39;2 * 3 * 4 = &#39;. array_product($array);

$array= array(3,4,5);
echo &#39;<br>3 * 4 * 5 = &#39;. array_product($array);

$array= array(1,2,3,4,5,6,7,8,9,10);
echo &#39;<br>1 * 2 * 3 *...* 9 * 10 = &#39;. array_product($array);
?>

Output result:

What function is used to specify the sum of a column in a php array?

If there are non-numeric type elements in $array, then PHP will convert them into a Numeric value (implicit data type conversion), if the conversion fails, it will be treated as 0 value.

<?php
$array= array(2,"3.1",4);
echo &#39;2 * 3.1 * 4 = &#39;. array_product($array);

$array= array(3,"10.abc",5);
echo &#39;<br>3 * "10.abc" * 5 = 3 * 10 * 5 =&#39;. array_product($array);

$array= array(3,"hello",5);
echo &#39;<br>3 * "hello" * 5 = 3 * 0 * 5 =&#39;. array_product($array);
?>
  • The string "3.1" will be converted into a decimal 3.1, so the product of the elements of the first array is 24.8.

  • #The string "10abc" will be converted to the integer 10, so the product of the elements of the second array is 150.

  • The string "hello" cannot be converted to an integer, so as a value of 0, the product of the elements of the third array is 0.

Output result:

What function is used to specify the sum of a column in a php array?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What function is used to specify the sum of a column in a 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