PHP keep track of the last x iterations - php

I have four variables: $one_flag, $two_flag, $three_flag, and $four_flag. I'm using them as flags inside a for loop because I want to keep track of the last four iterations.
$one_flag = 1;
$two_flag = 0;
$three_flag = 0;
$four_flag = 0;
$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
foreach ($a as $v) {
if ($one_flag){
$first_v = $v;
$one_flag = 0;
$two_flag = 1;
}
if ($two_flag){
$second_v = $v;
$two_flag = 0;
$three_flag = 1;
}
if ($three_flag){
$third_v = $v;
$three_flag = 0;
$four_flag = 1;
}
if ($four_flag){
$fourth_v = $v;
$four_flag = 0;
$first_flag = 1;
}
if ($v == 45){
# tricky part
print "The last three v's were: " . $first_v . ", " . $second_v . ", " . $third_v . "\n";
}
}
Right now it prints The last three v's were 1, 1, 1 but it's supposed to print The last three v's were 45, 44, 27. Also, the problem with the code above is I need to know which flag is equal to 1 (what flag we're currently on) so that I can print the correct statement.
For example, when $v == 45, $first_v is suppose to equal to 27; $second_v is suppose to equal to 44; $fourth_v is suppose to equal to 17; and third_v is suppose equal to 45. I'd need to know $three_flag is equal to 1 in order to print out "The last three were $second_flag, $first_flag, $fourth_flag" in that order.
How can I get my loop to work? How do I keep track of the last four iterations?
EDIT: I misspoke. I actually want to print The last three v's were 44, 27, 17

I suppose that this is a minimal example and that the real program is more complex. Otherwise, you could just get the length of the array and retrieve the last 3 elements based on it.
I'd change the approach as follow:
$f1 = -1; // Last value read
$f2 = -1; // second to last value
$f3 = -1;
$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
foreach ($a as $v) {
$f3 = $f2;
$f2 = $f1;
$f1 = $v;
if ($v == 45){
# tricky part
print "The last three v's were: " . $f1 . ", " . $f2 . ", " . $f3 . "\n";
}
}
I'm using -1 as an indicator that nothing was assigned to the variable. You should adapt this to your context if needed.

The problem lies in your if-statements.
//loop sets $v = 1
//this one is true due to initial parameters, execute
if ($one_flag){
$first_v = $v; //$first_v = 1
$one_flag = 0;
$two_flag = 1; //$two_flag is now TRUE!!!
}
//$two_flag is true, execute immediately!
if ($two_flag){
$second_v = $v; //$second_v = 1
$two_flag = 0;
$three_flag = 1;
}
Although in my reading, that should cause the print to give The last three v's were 45, 45, 45. If you want to keep doing it like this, you will need to use else if.

$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
$track = array();
$num = 3;
foreach ($a as $v){
$track[] = $v;
if (count($track) > $num){
$track = array_slice($track, 1);
}
if ($v == 45){
print_r(array_reverse($track));
}
}
result
Array
(
[0] => 45
[1] => 44
[2] => 27
)

It'd help to know the problem you're trying to solve, but off-hand I'd use a circular array to keep track of the last four. (Edited per your revision):
$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
$previous = array ();
$pCounter = 0;
foreach ($a as $i => $v) {
if (45 === $v) {
print_r(array_reverse($previous));
}
// track the last three using a circular array
$previous[$pCounter] = $v;
$pCounter = ($pCounter + 1) % 3;
}
Results in:
Array
(
[0] => 44
[1] => 27
[2] => 17
)
Try it online.

instead of if use elseif, it will serve your purpose, and change the variable name $first_flag to $one_flag.
$one_flag = 1;
$two_flag = 0;
$three_flag = 0;
$four_flag = 0;
$probe_counter = 1;
$first_v=0;
$second_v=0;
$third_v=0;$fourth_v=0;
$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
foreach ($a as $key => $val) {
if ($one_flag){
$first_v = $val;
$one_flag = 0;
$two_flag = 1;
}
elseif ($two_flag){
$second_v = $val;
$two_flag = 0;
$three_flag = 1;
}
elseif ($three_flag){
$third_v = $val;
$three_flag = 0;
$four_flag = 1;
}
elseif ($four_flag){
$fourth_v = $val;
$four_flag = 0;
$one_flag = 1;
}
if ($val == 45){
# tricky part
echo "The last three v's were: " . $first_v . ", " . $second_v . ", " . $third_v . "<br>";
}
}

Related

How to condense an array of integers in PHP?

So I have an array of integers: <1, 2, 3, 9, 10, 11, 14>, that I would like to join together in this format: <1-3, 9-11, 14>.
I'm new to PHP and tried doing this by looping through the array:
function pasteTogether($val)
{
$newVals = array();
$min = $val[0];
$max = $val[1];
$counter = 0;
for ($i = 0; $i < count($val); $i++)
{
if ($val[$i + 1] === $val[$i] + 1)
{
$max = $val[$i + 1];
}
else
{
$tempVal = $min."-".$max;
$newVals[$counter] = $tempVal;
$counter++;
$min = $val[$i];
}
}
return $newVals;
}
However, when I run this code, I get <1-3, 3-11, 11-11, 14-14>
PHP Fatal error: Maximum execution time of 30 seconds exceeded in ../learning.php on line 36
Because the for loop never ends you increment $val instead of $i
$array = array(1, 2, 3, 9, 10, 11, 14);
function pasteTogether($val)
{
$newVals = array();
$min = $val[0];
$max = $val[1];
$counter = 0;
for ($i = 0; $i < count($val); $i++)
{
if ($val[$i + 1] === $val[$i] + 1)
{
$max = $val[$i + 1];
}
else
{
$tempVal = $min."-".$max;
$newVals[$counter] = $tempVal;
$counter++;
$min = $val[$i];
}
}
return $newVals;
}
pasteTogether($array);
I have been playing around with this interesting problem and found another solution. So, if anyone is interested:
$arr=array(1, 2, 3, 9, 10, 11, 14, 15, 16, 18);
$v0=$dif=null;$rows=array();
foreach ($arr as $i => $v) {
if ($dif!=($d=($v-$i))){
if ($v0) $rows[]="$v0-".$arr[$i-1];
$v0=$v;
$dif=$d;
}
}
$rows[]="$v0-".($d==$dif?$arr[$i]:$v0);
print_r($rows);
I added a few numbers to the array and the result is this:
$rows = Array
(
[0] => 1-3
[1] => 9-11
[2] => 14-16
[3] => 18-18
)
You can find a little demo here: http://rextester.com/ABC25608
This works:
function pasteTogether($val)
{
$compacted = [];
$min = null;
$max = null;
$format = function ($a, $b) {
return ($a < $b ? "$a-$b" : $a);
};
foreach ($val as $current) {
if ($min === null) {
$min = $current;
$max = $current - 1;
}
if ($current == $max + 1) {
$max++;
} else {
$compacted[] = $format($min, $max);
$min = $current;
$max = $current;
}
}
$compacted[] = $format($min, $max);
return $compacted;
}
echo '<', implode(', ', pasteTogether([1, 2, 3, 9, 10, 11, 14])), '>';
Output:
<1-3, 9-11, 14>

PHP - counting in a single variable the difference in operations between two values of arrays

My problem is when I count the "wins" of a player , it counts every wins in the previous value and the current one.
More detailed , I have this values
$a0 = 10;
$a1 = 12;
$a2 = 14;
$b0 = 20;
$b1 = 20;
$b2 = 10;
$arr1 = array($a0, $a1, $a2); // Martina's numbers
$arr2 = array($b0, $b1, $b2); // George's numbers
foreach($arr1 as $key => $val){
if($val > $arr2[$key]){ // Martina win , +1 point for Martina
$martina++;
print($martina . " ");
}elseif($val < $arr2[$key]){ // George win , +1 point for George
$george++;
print($george . " ");
}else{ // if is Equal - no score increase
print("");
}
}
The score between them it must to be 1 2 for George in this case , but my code outputs also the score from first win , and the additioned score.
How can I make it to trow me only the additioned score.. ?
Hope that you understand my explanation of the problem , I'm at beginning on this language.
Move prints outside of foreach
$arr1 = array(10, 12, 14); // Martina's numbers
$arr2 = array(20, 20, 10); // George's numbers
$alice = 0;
$bob = 0;
foreach($arr1 as $key => $val) {
if($val > $arr2[$key]) {
// Martina win , +1 point for Martina
$alice++;
} elseif($val < $arr2[$key]) {
// George win , +1 point for George
$bob++;
}
}
print('Alice: ' . $alice . PHP_EOL);
print('Bob: ' . $bob . PHP_EOL);
Finally I did it !!
Thank you very much E_p for your help to solving this problem.
The final and the right code is :
<?php
// Values of Martina's numbers
$a0 = 21;
$a1 = 2;
$a2 = 32;
//Values of George's numbers
$b0 = 22;
$b1 = 3;
$b2 = 13;
//Putting the values into arrays
$arr1 = array($a0, $a1, $a2);
$arr2 = array($b0, $b1, $b2);
// Given two variables for players score
$martina = 0;
$george = 0;
foreach($arr1 as $key => $val){
if($val > $arr2[$key]){
$martina++;
}elseif($val < $arr2[$key]){
$george++;
}
}
print($martina . " " . $george . PHP_EOL);

how to find highest and second highest number in an array without using max function

I already have solution. But i think it will be more optimizable. So please provide me a solution for it. And remember that don't use predefined function of php. Like max() function.
i know there are so many ways to find it but i want best and better solution. Because my array contains more than 1 lakh records and it's taking lot of time. Or sometime site will be down.
My code :
<?php
$array = array('1', '15', '2','10',4);
echo "<pre>";
print_r($array);
echo "<pre>";
$max = 0;
$s_max=0;
for($i=0; $i<count($array); $i++)
{
$a = $array[$i];
$tmax = $max;
$ts_max = $s_max;
if($a > $tmax && $a > $ts_max)
{
$max = $a;
if($tmax > $ts_max) {
$s_max = $tmax;
} else {
$s_max = $ts_max;
}
} else if($tmax > $a && $tmax > $ts_max)
{
$max = $tmax;
if($a > $ts_max) {
$s_max = $a;
} else {
$s_max = $ts_max;
}
} else if($ts_max > $a && $ts_max > $tmax)
{
$max = $ts_max;
if($a > $tmax)
{
$s_max = $a;
} else {
$s_max = $tmax;
}
}
}
echo "Max=".$max;
echo "<br />";
echo "S_max=".$s_max;
echo "<br />";
?>
<?php
$array = array('200', '15','69','122','50','201');
$max_1 = $max_2 = 0;
for ($i=0; $i<count($array); $i++) {
if ($array[$i] > $max_1) {
$max_2 = $max_1;
$max_1 = $array[$i];
} else if ($array[$i] > $max_2 && $array[$i] != $max_2) {
$max_2 = $array[$i];
}
}
echo "Max=".$max_1;
echo "<br />";
echo "Smax 2=".$max_2;
See this solution.
<?php
$numbers = array_unique(array(1,15,2,10,4));
// rsort : sorts an array in reverse order (highest to lowest).
rsort($numbers);
echo 'Highest is -'.$numbers[0].', Second highest is -'.$numbers[1];
// O/P: Highest is -15, Second highest is -10
?>
I didn't check your solution, but in terms of complexity it's IMO optimal. If the array has no more structural information (like it's sorted) there's no way to skip entries. I.e. the best solution is in O(n) which your solution is.
This is a perfect and shortest code to find out the second largest value from the array. The below code will always return values in case the array contains only a value.
Example 1.
$arr = [5, 8, 1, 9, 24, 14, 36, 25, 78, 15, 37];
asort($arr);
$secondLargestVal = $arr[count($arr)-1];
//this will return 37
Example 2.
$arr = [5];
asort($arr);
$secondLargestVal = $arr[count($arr)-1];
//this will return 5
You can also use techniques in sorting like Bubble sort
function bubble_Sort($my_array )
{
do
{
$swapped = false;
for( $i = 0, $c = count( $my_array ) - 1; $i < $c; $i++ )
{
if( $my_array[$i] > $my_array[$i + 1] )
{
list( $my_array[$i + 1], $my_array[$i] ) =
array( $my_array[$i], $my_array[$i + 1] );
$swapped = true;
}
}
}
while( $swapped );
return $my_array;
}
$test_array = array(3, 0, 2, 5, -1, 4, 1);
echo "Original Array :\n";
echo implode(', ',$test_array );
echo "\nSorted Array\n:";
echo implode(', ',bubble_Sort($test_array)). PHP_EOL;
Original Array :
3, 0, 2, 5, -1, 4, 1
Sorted Array :
-1, 0, 1, 2, 3, 4, 5
Flow explanation
$array = array(80,250,30,40,90,10,50,60,50); // 250 2-times
$max = $max2 = 0;
foreach ($array as $key =>$val) {
if ($max < $val) {
$max2 = $max;
$max = $val;
} elseif ($max2 < $val && $max != $val) {
$max2 = $val;
}
}
echo "Highest Value is : " . $max . "<br/>"; //output: 250
echo "Second highest value is : " . $max2 . "<br/>"; //output: 90
The answer given by "Kanishka Panamaldeniya" is fine for highest value but will fail on second highest value i.e. if array has 2-similar highest value, then it will showing both Highest and second highest value same. I have sorted out it by adding one more level comparsion and it works fine.
$array = array(50,250,30,250,40,70,10,50); // 250 2-times
$max=$max2=0;
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] > $max) {
$max2 = $max;
$max = $array[$i];
} else if (($array[$i] > $max2) && ($array[$i] != $max)) {
$max2 = $array[$i];
}
}
echo "Highest Value is : " . $max . "<br/>"; //output : 250
echo "Second highest value is : " . $max2 . "<br/>"; //output : 70
This code will return second max value from array
$array = array(80,250,30,250,40,90,10,50,60,50); // 250 2-times
$max=$max2=0;
for ($i = 0; $i < count($array); $i++) {
if($i == 0) {
$max2 = $array[$i];
}
if($array[$i] > $max) {
$max = $array[$i];
}
if($max > $array[$i] && $array[$i] > $max2) {
$max2 = $array[$i];
}
}
echo "Highest Value is : " . $max . "<br/>"; //output : 250
echo "Second highest value is : " . $max2 . "<br/>"; //output : 90
Two way find the second highest salary
1. Sort the data in Descending order
2. get array first value
3. Check the condition already comments
$array = [2,3,6,11,17,14,19];
$max = $array[0];
$count = count($array);
for($i=0; $i<$count;$i++)
{
for($j=$i+1;$j<$count;$j++)
{
if($array[$i] < $array[$j])
{
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
First Method
//echo $array[1]; // Second highest value
$second ='';
for($k=0;$k<2;$k++)
{
if($array[$k] >$max)
{
$second = $array[$k];
}
}
echo $second; // Second method
Without using MAX function. Here.
$arr = [3,4,-5,-3,1,0,4,4,4];
rsort($arr); // SORT ARRAY IN DESCENDING ORDER
$largest = $arr[0]; // IN DESCENDING ORDER THE LARGEST ELEMENT IS ALWAYS THE FIRST ELEMENT
$secondLargest = 0;
foreach($arr as $val){
$secondLargest = $val; // KEEP UPDATING THE VARIABLE WITH THE VALUE UNTIL IT RECEIVES THE FIRST ELEMENT WHICH IS DIFFERENT FROM THE LARGEST VALUE
if($val != $arr[0]){
break; // BREAK OUT OF THE LOOP AS SOON AS THE VALUE IS DIFFERENT THAN THE LARGEST ELEMENT
}
}
echo $secondLargest;

Getting the most repeated values in an array [duplicate]

This question already has answers here:
PHP get the item in an array that has the most duplicates
(2 answers)
Closed 1 year ago.
I have an array of numbers like this:
$array = array(1,1,1,4,3,1);
How do I get the count of most repeated value?
This should work:
$count=array_count_values($array);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo "The most occuring value is $keys[0][1] with $keys[0][0] occurences."
I think array_count_values function can be useful to you. Look at this manual for details : http://php.net/manual/en/function.array-count-values.php
You can count the number of occurrences of values in an array with array_count_values:
$counts = array_count_values($array);
Then just do a reverse sort on the counts:
arsort($counts);
Then check the top value to get your mode.
$mode = key($counts);
If your array contains strings or integers only you can use array_count_values and arsort:
$array = array(1, 1, 1, 4, 3, 1);
$counts = array_count_values($array);
arsort($counts);
That would leave the most used element as the first one of $counts. You can get the count amount and value afterwards.
It is important to note that if there are several elements with the same amount of occurrences in the original array I can't say for sure which one you will get. Everything depends on the implementations of array_count_values and arsort. You will need to thoroughly test this to prevent bugs afterwards if you need any particular one, don't make any assumptions.
If you need any particular one, you'd may be better off not using arsort and write the reduction loop yourself.
$array = array(1, 1, 1, 4, 3, 1);
/* Our return values, with some useless defaults */
$max = 0;
$max_item = $array[0];
$counts = array_count_values($array);
foreach ($counts as $value => $amount) {
if ($amount > $max) {
$max = $amount;
$max_item = $value;
}
}
After the foreach loop, $max_item contains the last item that appears the most in the original array as long as array_count_values returns the elements in the order they are found (which appears to be the case based on the example of the documentation). You can get the first item to appear the most in your original array by using a non-strict comparison ($amount >= $max instead of $amount > $max).
You could even get all elements tied for the maximum amount of occurrences this way:
$array = array(1, 1, 1, 4, 3, 1);
/* Our return values */
$max = 0;
$max_items = array();
$counts = array_count_values($array);
foreach ($counts as $value => $amount) {
if ($amount > $max) {
$max = $amount;
$max_items = array($value);
} elif ($amount = $max) {
$max_items[] = $value;
}
}
$vals = array_count_values($arr);
asort($vals);
//you may need this end($vals);
echo key($vals);
I cant remember if asort sorts asc or desc by default, you can see the comment in the code.
<?php
$arrrand = '$arr = array(';
for ($i = 0; $i < 100000; $i++)
{
$arrrand .= rand(0, 1000) . ',';
}
$arrrand = substr($arrrand, 0, -1);
$arrrand .= ');';
eval($arrrand);
$start1 = microtime();
$count = array_count_values($arr);
$end1 = microtime();
echo $end1 - $start1;
echo '<br>';
$start2 = microtime();
$tmparr = array();
foreach ($arr as $key => $value);
{
if (isset($tmparr[$value]))
{
$tmparr[$value]++;
} else
{
$tmparr[$value] = 1;
}
}
$end2 = microtime();
echo $end2 - $start2;
Here check both solutions:
1 by array_count_values()
and one by hand.
<?php
$input = array(1,2,2,2,8,9);
$output = array();
$maxElement = 0;
for($i=0;$i<count($input);$i++) {
$count = 0;
for ($j = 0; $j < count($input); $j++) {
if ($input[$i] == $input[$j]) {
$count++;
}
}
if($count>$maxElement){
$maxElement = $count;
$a = $input[$i];
}
}
echo $a.' -> '.$maxElement;
The output will be 2 -> 3
$arrays = array(1, 2, 2, 2, 3, 1); // sample array
$count=array_count_values($arrays); // getting repeated value with count
asort($count); // sorting array
$key=key($count);
echo $arrays[$key]; // get most repeated value from array
String S;
Scanner in = new Scanner(System.in);
System.out.println("Enter the String: ");
S = in.nextLine();
int count =1;
int max = 1;
char maxChar=S.charAt(0);
for(int i=1; i <S.length(); i++)
{
count = S.charAt(i) == S.charAt(i - 1) ? (count + 1):1;
if(count > max)
{
max = count;
maxChar = S.charAt(i);
}
}
System.out.println("Longest run: "+max+", for the character "+maxChar);
here is the solution
class TestClass {
public $keyVal;
public $keyPlace = 0;
//put your code here
public function maxused_num($array) {
$temp = array();
$tempval = array();
$r = 0;
for ($i = 0; $i <= count($array) - 1; $i++) {
$r = 0;
for ($j = 0; $j <= count($array) - 1; $j++) {
if ($array[$i] == $array[$j]) {
$r = $r + 1;
}
}
$tempval[$i] = $r;
$temp[$i] = $array[$i];
}
//fetch max value
$max = 0;
for ($i = 0; $i <= count($tempval) - 1; $i++) {
if ($tempval[$i] > $max) {
$max = $tempval[$i];
}
}
//get value
for ($i = 0; $i <= count($tempval) - 1; $i++) {
if ($tempval[$i] == $max) {
$this->keyVal = $tempval[$i];
$this->keyPlace = $i;
break;
}
}
// 1.place holder on array $this->keyPlace;
// 2.number of reapeats $this->keyVal;
return $array[$this->keyPlace];
}
}
$catch = new TestClass();
$array = array(1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 1, 2, 3, 1, 1, 2, 5, 7, 1, 9, 0, 11, 22, 1, 1, 22, 22, 35, 66, 1, 1, 1);
echo $catch->maxused_num($array);

Remove invalid numbers (too big, too small) from array

I have an array of numbers and some numbers are obviously too big or too small relative to them all. I wonder if there is already some kind of function or algorithm that I can use in order to remove these records from array.
Here is an example of array
8
7
21
1330829238608
6
7
188
8
25
92433
19
6
At the moment all I can think about is just check if number is more than 1k or less than 1k and then do not allow it. But still I get problem since 188 does not belong here either.
Is there any good way that I can get majority of close numbers from this array and produce something like
8
7
6
7
8
6
This is what I have so far
<?php
echo '<pre>';
$startArray = Array(8, 7, 21, 1330829238608, 6, 7, 188, 8, 25, 92433, 19, 6);
print_r($startArray);
for ($i = 0; $i < count($startArray); $i++) {
if ($i != count($startArray) - 1) {
if ($startArray[$i] - 10 <= $startArray[$i + 1]) {
echo $startArray[$i] . '<br />';
}
}
}
Use array_filter:
function filter_callback($var){
return $var < 100 && $var > 2;
}
$array = array(1,1000,23,4453,123,412,321,433,4,6,2,3,5,634,32,432,45,3,4);
$filtered = array_filter($array, "filter_callback");
$arrayData = array(8, 7, 21,
1330829238608,
6, 7, 188, 8, 25,
92433,
19, 6,
);
$min = 7;
$max = 10;
$matches = array_filter( $arrayData,
function($data) use ($min,$max) {
return (($data >= $min) && ($data <= $max));
}
);
var_dump($matches);
I really gotta go but it's an easy one.
create a maximum = closest number to 0 in the array, multiplied by $delta.
any number in the array smaller than the maximum from pct. 1, but if multiplied by $delta is greater than that maximum, becomes maximum if($i<$max && $i*$delta>$max) $max = $i*$delta
trim out all numbers bigger than the current maximum.
Problem is you need a $delta. It's safe to go with 2 for delta, but if you want it adaptable don't ask me how to do it because that's cognitive neuroscience.
This can be optimized but this is what I figured out, works with diff of 20%, you can change it to whatever % you want, of course.
<?php
$startArray = array(8, 7, 21, 1330829238608, 6, 7, 188, 8, 25, 92433, 19, 6);
$groupArray = array();
$startArrayCount = count($startArray);
for ($i = 0; $i < $startArrayCount; $i++) {
// 20% of current value
$valueDiff = ($startArray[$i] / 100) * 20;
// Get minimal and maximal value
$maxValue = ($startArray[$i] + $valueDiff);
$minValue = ($startArray[$i] - $valueDiff);
// Print it out
// echo 'Diff: ' . $valueDiff . '<br />';
// echo 'Max: ' . $maxValue . '<br />';
// echo 'Min: ' . $minValue . '<br />';
$groupArray[$i] = array();
for ($n = 0; $n < $startArrayCount; $n++) {
if ($startArray[$n] <= $maxValue && $startArray[$n] >= $minValue) {
// echo 'TRUE: ' . $startArray[$n] . '<br />';
array_push($groupArray[$i], $startArray[$n]);
}
}
//echo '<hr />';
}
// Getting arrays that have most members in it
$max = count($groupArray[0]);
foreach ($groupArray as $group) {
if (count($group) > $max) {
$max = count($group);
}
}
// Taking all those arrays and combining them in one
$finishArray = array();
foreach ($groupArray as $group) {
if (count($group) == $max) {
foreach ($group as $key) {
array_push($finishArray, $key);
}
}
}
// Combining all values
$total = null;
foreach ($finishArray as $num) {
$total = $total + $num;
}
// Getting average
$average = $total / count($finishArray);
echo $average;

Categories