Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to multiply arrays and sum by php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to repeat arrays and sum" in php. In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Deduplicated array summation method: 1, use array_unique () to remove duplicates, syntax "array_unique (array)", return a new array with no duplicate values; 2, use array_sum () for summation, syntax "array_sum (de-duplicated array)", return the sum of all values in the de-duplicated array.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The php de-duplicated array sum can be divided into two steps:

Step 1. Use the array_unique () function to remove the weight of the array

The array_unique () function removes duplicate values from the array. If two or more array values are the same, only the first value is retained and the other values are removed.

Note: the retained array will retain the key name type of the first array item.

Step 2. Use array_sum () to sum

$sum=array_sum ($result); echo "array deduplicated and summed:". $sum

Description:

The array_sum ($arr) function is used to calculate the sum of all elements in an array

Returns an integer value if all elements of the array arr are integers, or floating-point numbers if one or more of the values are floating-point numbers.

If there are non-numeric elements in the array arr, PHP attempts to convert them to a numeric value, which fails as a value of 0.

This is the end of "how to multiply the array and sum the php". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report