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 6234555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:30:30+00:00 2026-05-24T10:30:30+00:00

here is my array sample $data[][] ; Array( [0] => Array ( [id] =>

  • 0

here is my array sample $data[][];

Array( [0] => Array ( [id] => 1349
                      [rating1] => 1.9378838029981E-7
                      [rating2] => 1.1801796607774     )
       [1] => Array ( [id] => 1350
                      [rating1] => 5.5499981876923E-7
                      [rating2] => 1.5121329727308     )
       [2] => Array ( [id] => 1377
                      [rating1] => 0.00023952225410117
                      [rating2] => 2.1947077830236     )
       [3] => Array ( [id] => 1378
                      [rating1] => 0.00022982302863634
                      [rating2] => 2.2135588326622     )
       [4] => Array ( [id] => 1379
                      [rating1] => 0.00026272979843585
                      [rating2] => 2.2388295595073     )
       [5] => Array ( [id] => 1380
                      [rating1] => 0.0002788640872546
                      [rating2] => 2.1815325502993     )
)

I want to find max($data[][rating?]) but return $data[id][max_rating?] i.e. id associated with the max value.
Finding the max was easy for one particular, say rating1, I used array_reduce as follows (this is inspired from this SO ):

$max = array_reduce($data, function($a, $b) {
    return $a > $b['rating1'] ? $a : $b['rating1']; 
});

Now I have two questions :
1. How can I extend above array_reduce to include rating2 ? I have other ratingX as well.
2. I do not want the max value, rather the $data[][id] associated with the max.

I am not so much concerned about Q1, but the second one is important as I don’t want to search through the array again to get associated $data[][id].
One line of thought is to use array_map instead of array_reduce, but I couldn’t come up with a version which will pass on both [id] and [rating?]. Also, there are complications when I try to max() multiple rating? at one shot, as each rating will have different max, which in turn associates with different [id].
EDIT : Just to be clear, I want all the respective ids associated with respective max of each rating?

  • 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-05-24T10:30:31+00:00Added an answer on May 24, 2026 at 10:30 am

    assuming your array is unsorted you have to loop through it at least once (either manually or using builtin functions). i’d use the following code:

    $array = array(
      array( 'id' => 1349, 'sudhi_rating1' => 1.9378838029981E-7, 'sudhi_rating2' => 1.1801796607774 ),
      array( /* … */ ),
      /* … */
    );
    
    $idx_max = 0;
    foreach($array as $idx => $item) {
      if($item['sudhi_rating1'] > $array[$idx_max]['sudhi_rating1'])
        $idx_max = $idx;
    }
    
    echo "Rating 1 has max value at id ", htmlspecialchars($array[$idx_max]['id']);
    

    you can extend the code to check multiple ratings at once (make $idx_max an array itself and add more ifs):

    $idx_max = array (
      'sudhi_rating1' => 0,
      'sudhi_rating2' => 0,
      /* … */ );
    foreach($array as $idx => $item) {
      foreach($idx_max as $rating => &$max) {
        if($item[$rating] > $array[$max][$rating])
          $max = $idx;
      }
    }
    
    foreach($idx_max as $rating => $max)
      echo 'Max value for ', htmlspecialchars($rating), ' has id ', htmlspeciachars($array[$max]['id']);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample Data : input : abcdacdc Output : cadb here we have to sort
Simple question here: is there any way to convert from a jagged array to
Here's my array: [2555] => Array ( [0] => stdClass Object ( [meta_id] =>
Here is the code: $arraya = array('a','b','c'); foreach($arraya as $key=>$value) { if($value == 'b')
Here is my code: - (void)loadView { //hard coded array of content for each
Here is pseudo-code of how I setup an array representing the MandelBrot set, yet
Here's an example of what I mean: I have an array: array('type'=>'text', 'class'=>'input', 'name'=>'username',
Here is my issue. I have an array containing the name of cities that
here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
Here is my code, which creates 2d array filled with zeros, array dimensions are

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.