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

  • SEARCH
  • Home
  • 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 8457937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:56:31+00:00 2026-06-10T12:56:31+00:00

My requirement is to find the greatest/max value in an array, which may contain

  • 0

My requirement is to find the greatest/max value in an array, which may contain other arrays within it. For example we could have a look at an array below.

$array =
    array(
        13,
        array(10, 4, 111, 3),
        4,
        array(23, 450, 12,array(110, 119, 20, 670), 45 ,45,67,89),
        );

$max = find_max($array, 0);

print "Maximumum Value is  $max";

I already have a working function find_max, but all I wanted to know is what could the best and efficient possible way to do this other than the code given below.

function find_max($array, $maxValue) {
    foreach ($array as $member) {
        if (is_array($member)) {
            $maxValue = find_max($member, $maxValue);
        } else {

            if($member==$maxValue){
                continue;
            }
            if ($member > $maxValue) {
                $maxValue = $member;
            }
        }
    }
    return $maxValue;
}
  • 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-10T12:56:33+00:00Added an answer on June 10, 2026 at 12:56 pm

    Finding the max value requires O(n), so you can’t improve it drastically, as far as I know.
    But, you can add a minor improvement to your code:

    function find_max($array, $maxValue) {
        foreach ($array as $member) {
            if (is_array($member)) {
                $maxValue = find_max($member, $maxValue);
            } else {
                if ($member > $maxValue) {
                    $maxValue = $member;
                }
            }
        }
        return $maxValue;
    }
    
    $array =
    array(
        13,
        array(10, 4, 111, 3),
        4,
        array(23, 450, 12,array(110, 119, 20, 670), 45 ,45,67,89),
        );
    $ans = find_max($array, 0);
    echo "ans = $ans";
    

    output: 670

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one intresting requirement to achieve. could you please help? Please find the
I am having some requirement in which I have to find out if the
I have got the requirement to find the core file in multiple box/machine and
I have the following Repository Pattern. Requirement is to Find All accounts whose owner’s
I have some business requirements which sound something like this: Find the data in
My requirement is to find a duplicate number in an array of integers of
Hi I have a very unique requirement. I need to find out a text
Requirement : To find out which phone the user is using by parsing the
I have requirement to keep datagridcell always in edit mode. I dont find any
I have a requirement to find items in a Lucene index that have two

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.