Home php Calculate the amount of the elements of the array

# Calculate the amount of the elements of the array

Author

Date

Category

I want to write a function on PHP that can calculate the amount of array elements. The length of the array is set by the argument of this function.

tried to write, obviously not that Help!

``````function get_sum (\$ arr = 100) {
\$ Sum = 0;
For (\$ i = 0; \$ i & lt; = \$ arr []; \$ i ++) {
Echo \$ i;
}
\$ Sum = \$ Sum + \$ ArR [\$ i];
Echo \$ Sum;
}
``````

## 1, Authority 100%

I did not quite understand the essence of your question, but if you want to write a function that considers the amount of array elements, which is transmitted to this function, then you need to do this:

``````function get_sum (\$ arr) {
\$ Sum = 0;
Foreach (\$ AR AS \$ Elem)
\$ Sum + = \$ Elem;
Return \$ Sum;
}
``````

An example of calling a function:

``````\$ values ​​= array (1,2,5,100, -30);
Echo get_sum (\$ values); // withdraw 78.
``````

## 2, Authority 100%

The length of the array is set by the argument of this function.

This phrase seems to me ambiguous. It is understood that the only argument of the F-AI – & NBSP; the array itself, and on it determine its length? Or two parameters are transmitted: the array itself and the number of elements that should be summed up, and it may be less, equal, or even more than the length of the array, and all these cases need to be processed?

Focusing on the simplest explanation, only array itself is transmitted.

``````function get_sum (\$ arr) {// removed the default value (= 100)
// - An array is expected here, not the number
\$ Sum = 0; // OK, initialize the amount
For (\$ i = 0; \$ I & LT; Count (\$ ARR); \$ i ++) {// Length of array: Count (\$ ARR)
// Watch the items from 0 and to length_minus_1:
// for example array [0,1,2] and its length 3. Therefore, the top value
// \$ i & lt; 3.
\$ Sum = \$ Sum + \$ ArR [\$ i]; // To the total amount it is necessary to add the next value from the array - & nbsp; inside the cycle, and not outside
}
Return \$ Sum; // makes sense so that the F-I returned the result,
// And what to do with it further - to display or something else
// Let them decide there, outside the F-AI.
}
// Now it is necessary to somehow use this feature:
echo "sum of the array [1,2,3] =";
echo get_sum (Array (1,2,3));
Echo PHP_EOL; // new string symbol
``````

p.s. And if this is not a learning task, in PHP there is built-in function , which summarizes all elements of this array faster, `array_sum () `:

``````echo "Amount [1,2,3] =". array_sum ([1,2,3]). Php_eol;
``````

## 3, Authority 67%

1. Write a signature of a method that takes an array of numbers
2. declare a variable for summation, the value `0 `.
3. go through the array using the for cycle with zero index to the array length, or foreach
4. on each iteration to add to the sum of the i-th element.
After the cycle is completed, return the result.

## 4, Authority 33%

We consider the amount of the elements of an array of \$ arR [] from 0 to \$ n, where \$ n is the function parameter

``````function get_sum (Array \$ Arr, \$ n) {
\$ Sum = 0;
For (\$ i = 0; \$ i & lt; = \$ n; \$ i ++) {
\$ Sum + = \$ Arr [\$ i];
}
Return \$ Sum;
``````

}

\$ arr = [2, 4, 5, 6, 4, 5];
\$ n = 4;

echo get_sum (\$ arr, \$ n); // Release 21 folded elements with indexes from 0 th to 4th

## 5

``````& lt;? php
Function Calculatesum (\$ ARR)
{
If (Empty (\$ ARR)) {
RETURN NULL;
}
\$ Sum = 0;
For (\$ Sum As \$ NUM) {
\$ Sum + = \$ Sum;
}
Return \$ Sum;
}
``````

# Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.