我希望我已经很好地理解了你想要的东西:
你的数组:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
创建一个$people
包含$Mutt
和$Jeff
(通过引用传递)的新数组。
$people=array(&$Mutt,&$Jeff);
创建函数findMaxIndex
,它返回索引,该索引$people[that index]
是我们想要的位置处具有最大值的数组。
它的论点是:
$arr
,包含我们要比较的数组的数组(在这种情况下,$people
)
$pos1
和$pos2
,这是我们要比较的索引
所以...我们将比较
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
- ...
$arr[count($arr)-1][$pos1][$pos2]
这个函数是这样工作的:
- 它创建了数组
$max
,其中$max[0]
是具有最大值的 $arr 的索引(在我们检查过的数组中,直到那一刻),并且$max[1]
是那个值。
- 它遍历所有
$arr
- 如果发现当前值 (
$arr[$i][$pos1][$pos2]
) 大于最大值,$max
则更新并变为
array($i,$arr[$i][$pos1][$pos2])
。
- 最后,它返回
$max[0]
,它是
$people[that index]
我们想要的位置处具有最大值的数组的索引。
功能是:
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);
...给出0
,所以具有最大值的数组是$people[0]
( $Mutt
)
最后,我们增加该数组:
$people[$maxIndex][1]++;
$Mutt
并且$Jeff
也被修改了,因为我们通过引用传递了它们。
简而言之,
$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]++;
===============================================
如果您想要多个索引以防出现平局(更改为粗体):
你的数组:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
创建一个新数组$people
,其中包含$Mutt
和$Jeff
$people=array(&$Mutt,&$Jeff);
创建函数findMaxIndex
,它返回包含索引的数组,该数组的索引$people[that index]
是在我们想要的位置处具有最大值的数组。
它的论点是:
$arr
,包含我们要比较的数组的数组(在这种情况下,$people
)
$pos1
和$pos2
,这是我们要比较的索引
所以...我们将比较
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
- ...
$arr[count($arr)-1][$pos1][$pos2]
这个函数是这样工作的:
- 它创建了数组
$max
,其中$max[0]
是一个数组,其中包含具有最大值的 $arr的索引$max[1]
(在我们检查过的数组中,直到那一刻),并且是那个值。
- 它遍历所有
$arr
- 如果发现当前值 (
$arr[$i][$pos1][$pos2]
) 大于最大值,$max
则更新并变为
array(array($i),$arr[$i][$pos1][$pos2])
。
- 如果不是,并且如果它发现当前值(
$arr[$i][$pos1][$pos2]
)等于最大值,$max[0]
则更新并$i
推入其中。
- 最后,它返回
$max[0]
,它是包含索引的数组,该数组的索引
$people[that index]
是在我们想要的位置处具有最大值的数组。
功能是:
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);
...给出array(0,1)
,因此具有最大值的数组是$people[0]
( $Mutt
) 和$people[1]
( $Jeff
)。
最后,我们增加数组:
for($i=0;$i<count($maxIndex);$i++){
$people[$maxIndex[$i]][1]++;
}
$Mutt
并且$Jeff
也被修改了,因为我们通过引用传递了它们。
简而言之,
$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]++;
}