In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "PHP array operation of the calculation of all the elements of the array and the method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "PHP array operation of the calculation of all the elements of the array and the method is what" it!
Through the for loop
In our previous article, "how to use For Loop and Foreach Loop in PHP", we introduced the for Loop statement. Interested partners can click on the link to view it. Let's directly look at the example of how to calculate the sum of all elements through the for loop. The example is as follows:
Output result:
So we use the for loop to calculate the array elements and, but it should be noted that if there is no numerical element in the array calculated in the for loop, although the program will treat it as zero and the output is normal, the output will have an error display.
Next, let's take a look at the foreach loop, which is similar to the for loop.
Through the foreach loop
In the article mentioned above, we introduced not only the for loop but also the foreach loop. Let's not make too much statement below, but let's take a look at how to calculate array elements and through the use of the foreach function. The example is as follows:
Output result:
In this way, we can calculate the element sum of the array through foreach. Adding array elements through the for loop and the foreach loop is always a bit complicated. There is a built-in function array_sum in PHP that can easily solve this problem. Let's take a look at how to calculate it through array_sum.
Array_sum () function
The sum of all elements in the array can be calculated through the array_sum function. The basic syntax format of the array_sum function is as follows:
Array_sum (array)
It is important to note that the parameter array is an array that needs to calculate the sum of all elements, and the result is returned as an integer or floating-point number. If the array is an empty array, the result is 0.
If the elements in this array are not all numeric, not numeric elements, PHP this function will try to convert the element to a numeric value, if not, then when the value is 0, such a conversion example, a string "10.1", not in the form of a numeric value, but a string, then the conversion will be in the form of a numerical value 10.1 to calculate the sum of elements.
Let's take a look at the application of the array_sum function through an example, as follows:
Output result:
In the above example, the array elements are all numeric values, so you can add the elements directly without conversion. Let's take a look at how the string is converted. The example is as follows:
Output result:
In the above example, the elements of the string are converted, the string "0.01" is directly converted to the value 0.01, and there are some convertible and unconvertible array elements, as shown in the following example:
Output result:
Through the above example, some strings can be converted, such as "24k" in the example, but those like "argrg" cannot be converted and can only be treated as a value of 0. Let's write down all three methods. Examples are as follows:
Output result:
Thank you for your reading, the above is the "PHP array operation of the calculation of all the elements of the array and what is the method" of the content, after the study of this article, I believe you on the operation of the PHP array of all elements and the method of what is this problem has a more profound understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.