是否有在数组之间进行选择的方法,即特定位置具有最高值的数组


is there method for selecting between arrays, the array with the highest value for a specific position

路障:使用多个嵌套数组并希望在特定位置选择具有最大 int 的数组; 在所选数组中更改另一个特定位置的值。

伪代码可能如下所示:

$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1)
$Jeff = array($L, $P, $O, $array2)
find array with max [3][1] {
('selected' [1]++)
}

在现实生活中,这可能看起来像: 穆特($Mutt)本月工作天数最多($Mutt[3][1]), 他赢得了额外的"个人日"($Mutt[1])。

我试图找到解决这个问题的方法。我可能太新了,无法理解如何正确措辞我的搜索,但我没有运气。

我希望我已经很好地解决了你想要的东西:

您的阵列:

$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);

创建一个包含$Mutt$Jeff(通过引用传递)的新数组$people

$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]

此函数的工作方式如下:

  1. 它创建数组$max,其中$max[0]是$arr的索引具有最大值(在我们检查过的数组中,直到那时时刻),这个价值$max[1]
  2. 它遍历所有$arr
  3. 如果发现当前值 ( $arr[$i][$pos1][$pos2] ) 更大超过最大值,$max更新并变为 array($i,$arr[$i][$pos1][$pos2]) .
  4. 最后,它返回 $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);

创建一个包含$Mutt$Jeff的新数组$people

$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]

此函数的工作方式如下:

  1. 它创建数组$max,其中$max[0]是一个包含索引的数组$arr具有最大值(在我们检查过的数组中,直到那时时刻),$max[1]就是这个价值。
  2. 它遍历所有$arr
  3. 如果发现当前值 ( $arr[$i][$pos1][$pos2] ) 更大超过最大值,$max更新并变为 array(array($i),$arr[$i][$pos1][$pos2]) .
  4. 如果
  5. 不是,并且如果它发现当前值$arr[$i][$pos1][$pos2]等于最大值,则更新$max[0]并将$i推送到其中。
  6. 最后,它返回 $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]++;
}

我不确定您的最大值对整体问题有多重要。这看起来有点乱。我想到了 3 种不同的方法来解决这个问题。

  1. 创建和添加数组,该数组在每次数组突变时保持并更新最大值。这很快很脏,不是一个很好的解决方案,但取决于项目的超额价值可能是可以的。
  2. 编写一个函数来搜索最大值,过多但会起作用。
  3. 更高级但可能是最专业的解决方案,使用类重新设计您的程序。现在,每个数组都将成为其自己的对象。每个对象都可以使用其突变器在类变量中跟踪自己的最大值,或者添加函数随时更新它。现在,您可以简单地让一个由这些对象组成的数组,并且该数组可以轻松排序(使用该值的访问器按每个对象的最大值排序),然后只需根据排序方式从数组的顶部或底部拉出对象。您可能希望在类级别编写一个私有搜索函数,以在创建时找到最大的价值。