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:
Creating a new array
$peoplewhich contains$Muttand$Jeff(passed by reference).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)$pos1and$pos2, which are the indexes we want to compareSo… we will compare
$arr[0][$pos1][$pos2]$arr[1][$pos1][$pos2]$arr[count($arr)-1][$pos1][$pos2]This function works like this:
$max, where$max[0]is the index of $arrwith the maximum value (among the arrays we have examined until that
moment), and
$max[1]is that value.$arr$arr[$i][$pos1][$pos2]) is greaterthan the maximum value,
$maxis updated and becomesarray($i,$arr[$i][$pos1][$pos2]).$max[0], which is the index for which$people[that index]is the array with maximum value at theposition that we want.
The function is:
Then we call the function…
… which gives
0, so the array with maximum value is$people[0]($Mutt)Finally, we increase that array:
$Muttand$Jeffare modified too because we passed them by reference.In short,
==============================================
And if you want multiple indexes in case of tie (changes in bold):
Your arrays:
Creating a new array
$peoplewhich contains$Muttand$JeffCreating 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)$pos1and$pos2, which are the indexes we want to compareSo… we will compare
$arr[0][$pos1][$pos2]$arr[1][$pos1][$pos2]$arr[count($arr)-1][$pos1][$pos2]This function works like this:
$max, where$max[0]is an array which contains the indexes of $arrwith the maximum value (among the arrays we have examined until that
moment), and
$max[1]is that value.$arr$arr[$i][$pos1][$pos2]) is greaterthan the maximum value,
$maxis updated and becomesarray(array($i),$arr[$i][$pos1][$pos2]).$arr[$i][$pos1][$pos2]) equals the maximum value,$max[0]is updated and$iis pushed into it.$max[0], which is the array which contains the indexes for which$people[that index]is the array with maximum value at theposition that we want.
The function is:
Then we call the function…
… which gives
array(0,1), so the arrays with maximum value are$people[0]($Mutt) and$people[1]($Jeff).Finally, we increase the arrays:
$Muttand$Jeffare modified too because we passed them by reference.In short,