Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:27:05+00:00 2026-06-09T17:27:05+00:00

the roadblock: using multiple nested arrays and wanting to select the array with the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:27:06+00:00Added an answer on June 9, 2026 at 5:27 pm

    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]++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Mustache in Rails 3 with this gem and I'm hitting a roadblock
I'm running into a roadblock using InstallShield LE in VS2010. The second time running
Firstly, I'm rather new to SQL and I've run into a roadblock. I'm using
I'm stuck trying to get multiple textures working in OpenGL using Haskell. I've been
I want to add an additional roadblock in my application to prevent automating it
Where I'm running into a roadblock is trying to check for this (?<http>(http|ftp|https):\/\/)?(?<address>([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)
I am trying to learn how Python reloads modules, but have hit a roadblock.
I'm trying to figure out Elisp, and I've hit a roadblock. I want a
I'm trying to embed a PDF file into a Word document using the OLE
So, I'm writing something and I've come into a roadblock on how to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.