I hope I have undestood well what you want:
Your arrays:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
Creating a new array $people
which contains $Mutt
and $Jeff
(passed by reference).
$people=array(&$Mutt,&$Jeff);
Creating function findMaxIndex
, which returns the index for which $people[that index]
is the array with maximum value at the position that we want.
Its arguments are:
-
$arr
, the array which contains the arrays we want to compare (in this case, $people
)
-
$pos1
and $pos2
, which are the indexes we want to compare
So... we will compare
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
- ...
$arr[count($arr)-1][$pos1][$pos2]
This function works like this:
- It creates the array
$max
, where $max[0]
is the index of $arr
with the maximum value (among the arrays we have examined until that
moment), and $max[1]
is that value.
- It iterates through all
$arr
- If it finds that current value (
$arr[$i][$pos1][$pos2]
) is greater
than the maximum value, $max
is updated and becomes
array($i,$arr[$i][$pos1][$pos2])
.
- Finally, it returns
$max[0]
, which is the index for which
$people[that index]
is the array with maximum value at the
position that we want.
The function is:
function findMaxIndex($arr,$pos1,$pos2){
$max=array(0,$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
if($arr[$i][$pos1][$pos2]>$max[1]){
$max=array($i,$arr[$i][$pos1][$pos2]);
}
}
return $max[0];
}
Then we call the function...
$maxIndex=findMaxIndex($people,3,1);
... which gives 0
, so the array with maximum value is $people[0]
($Mutt
)
Finally, we increase that array:
$people[$maxIndex][1]++;
$Mutt
and $Jeff
are modified too because we passed them by reference.
In short,
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
$people=array(&$Mutt,&$Jeff);
function findMaxIndex($arr,$pos1,$pos2){
$max=array(0,$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
if($arr[$i][$pos1][$pos2]>$max[1]){
$max=array($i,$arr[$i][$pos1][$pos2]);
}
}
return $max[0];
}
$maxIndex=findMaxIndex($people,3,1);//gives `0` -> Max is `$people[0]`
$people[$maxIndex][1]++;
==============================================
And if you want multiple indexes in case of tie (changes in bold):
Your arrays:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
Creating a new array $people
which contains $Mutt
and $Jeff
$people=array(&$Mutt,&$Jeff);
Creating function findMaxIndex
, which returns the array which contains the indexes for which $people[that index]
is the array with maximum value at the position that we want.
Its arguments are:
-
$arr
, the array which contains the arrays we want to compare (in this case, $people
)
-
$pos1
and $pos2
, which are the indexes we want to compare
So... we will compare
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
- ...
$arr[count($arr)-1][$pos1][$pos2]
This function works like this:
- It creates the array
$max
, where $max[0]
is an array which contains the indexes of $arr
with the maximum value (among the arrays we have examined until that
moment), and $max[1]
is that value.
- It iterates through all
$arr
- If it finds that current value (
$arr[$i][$pos1][$pos2]
) is greater
than the maximum value, $max
is updated and becomes
array(array($i),$arr[$i][$pos1][$pos2])
.
-
If not, and if it finds that current value (
$arr[$i][$pos1][$pos2]
) equals the maximum value, $max[0]
is updated and $i
is pushed into it.
- Finally, it returns
$max[0]
, which is the array which contains the indexes for which
$people[that index]
is the array with maximum value at the
position that we want.
The function is:
function findMaxIndex($arr,$pos1,$pos2){
$max=array(array(0),$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
$current=$arr[$i][$pos1][$pos2];
if($current>$max[1]){
$max=array(array($i),$current);
}else if($current==$max[1]){
array_push($max[0],$i);
}
}
return $max[0];
}
Then we call the function...
$maxIndex=findMaxIndex($people,3,0);
... which gives array(0,1)
, so the arrays with maximum value are $people[0]
($Mutt
) and $people[1]
($Jeff
).
Finally, we increase the arrays:
for($i=0;$i<count($maxIndex);$i++){
$people[$maxIndex[$i]][1]++;
}
$Mutt
and $Jeff
are modified too because we passed them by reference.
In short,
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
$people=array(&$Mutt,&$Jeff);
function findMaxIndex($arr,$pos1,$pos2){
$max=array(array(0),$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
$current=$arr[$i][$pos1][$pos2];
if($current>$max[1]){
$max=array(array($i),$current);
}else if($current==$max[1]){
array_push($max[0],$i);
}
}
return $max[0];
}
$maxIndex=findMaxIndex($people,3,0);//gives `array(0,1)` -> Tie between `$people[0]` and `$people[1]`
for($i=0;$i<count($maxIndex);$i++){
$people[$maxIndex[$i]][1]++;
}