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

the roadblock: using multiple nested arrays and wanting to select the array with the largest int in a specific position; in the selected array change another specific position's value.

psuedo code might look like this:

$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]++)
}

in real life this might look like: Mutt ($Mutt) has the worked the most days this month ($Mutt [3][1]), he has earned an additional 'personal day' ($Mutt [1]).

I have tried to find a solution to this. I may be too new to understand how to word my search correctly, but I am having no luck.

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:

  1. 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.
  2. It iterates through all $arr
  3. 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]).
  4. 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:

  1. 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.
  2. It iterates through all $arr
  3. 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]).
  4. 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.
  5. 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]++;
}

I’m not sure how important your max value is to the overall problem. This looks kind of messy. I think of 3 different ways to handle this.

  1. Create and addition array that hold and updates the max value each time the arrayt is mutated. This is quick dirty and not a great solution but depending on over value of the project might be ok.
  2. Write a function to search for the highest value, excessive but will work.
  3. A little more advance but probably the most professional solution, redesign your program using classes. Each array will now become an object of its own. Each object can track its own max value in class variable with its mutator or add function updating it as you go. Now you can simply have one array made up of these objects and that array can be sorted easily (sort by the max value of each object using an accessor for that value) then just pull the object from the top or bottom of the array depending on which way you sorted it. You will probably want to write a private search function on the class level to find the greatest value on creation.