3

Here is the multi dimensional array

$arr = array(
 array(141,151,161), 
 2, 
 3, 
 array(
 101, 
 202, 
 array(303,404)
 )
);

How to find the highest value, which the result in the array above should be 404.
The array depth can may be more than 3.


Here is the code i last tried, but i notice this only can check until 3 depth, i need it can be check unlimited depth

function MaxArray($arr){
 foreach($arr as $valueDepth1){
 if(is_array($valueDepth1)){
 foreach($valueDepth1 as $valueDepth2){
 if(is_array($valueDepth2)){
 foreach($valueDepth2 as $valueDepth3){
 $checingArray[]=$valueDepth3;
 }
 }else{
 $checingArray[]=$valueDepth2;
 } 
 }
 }else{
 $checingArray[]=$valueDepth1;
 }
 }
 return max($checingArray);
}
asked Nov 10, 2011 at 21:14
4
  • This is not a give me the code site. Write a recursive function which calls itself, when the type of the child is an array. take a look at is_array(): php.net/manual/en/function.is-array.php Commented Nov 10, 2011 at 21:18
  • @Smamatti: yes i understand this is not a give code site. I tried to create the code. But cannot find the best logic to make the function. Commented Nov 10, 2011 at 21:20
  • @Smamatti: Thank you for the link, i already tried with that method before. The logic i didn't understand yet is Write a recursive function which calls itself like as you said. Commented Nov 10, 2011 at 21:22
  • @Marcus: i edited my question, thats is my last code. I purposely didn't write the code here in the first because i know it dull code and bad logic. Commented Nov 10, 2011 at 21:44

7 Answers 7

9
function highest($array) {
 foreach($array as $key => $value) {
 if (is_array($value)) {
 $array[$key] = highest($value);
 }
 }
 sort($array);
 return array_pop($array);
}

You can see it in action here: http://codepad.org/4xPFsU1U

answered Nov 10, 2011 at 21:27
2
  • thank you, the best logic i've seen. I didn't now if we can dow self method inside the function. Commented Nov 10, 2011 at 21:33
  • 2
    Calling the method itself within the method is called Recursion. It it very useful in many cases. Although you should be careful with it as for every recursive call you increase the stack size. Commented Nov 10, 2011 at 21:49
3

You could try something like this:

$max = -1;
array_walk_recursive($arr, 'arrmax');
function arrmax($item, $key) {
 global $max;
 $max = ($key > $max) ? $key : $max;
}
echo "Maximum number is : " . $max;
answered Nov 10, 2011 at 21:23
3
  • 2
    What if the value are all < -1 ? Commented Nov 10, 2011 at 21:24
  • I'd say: set $max to null at beginning, change conditional to (is_null($max) || $key > $max) Commented Nov 10, 2011 at 21:26
  • 1
    @Steve Or you can set $max to -INF Commented Nov 10, 2011 at 21:44
3

Here's a function I found on the first page on google. Search engines is a perfect place to find stuff ;)

function recursive_array_max($a) {
 foreach ($a as $value) {
 if (is_array($value)) {
 $value = recursive_array_max($value);
 }
 if (!(isset($max))) {
 $max = $value;
 } else {
 $max = $value > $max ? $value : $max;
 }
 }
 return $max;
}

Source

answered Nov 10, 2011 at 21:33
2

This can be a good starting point:

getMax($array)
{
 if ( is_array($array) )
 {
 // NOTE: PHP_MIN doesn't exist. Is just to let you understand the logic
 $max = PHP_MIN; 
 foreach($array as $value)
 {
 if ( is_array($value) )
 $value = getMax($value);
 if ($value > $max)
 $max = $value;
 }
 return $max;
 }
 else
 return $array
}
answered Nov 10, 2011 at 21:26
0

Try This

function MaxRecursive($arr,$maxVal = null)
{
 try {
 if(! isset($arr) || ! is_array($arr) )
 throw new Exception("The array is empty");
 for($i=0;$i<count($arr);$i++)
 {
 if(is_array($arr[$i]))
 $maxVal = MaxRecursive($arr[$i],$maxVal);
 else
 {
 if($maxVal == null)
 $maxVal = $arr[$i];
 if($arr[$i] > $maxVal )
 $maxVal = $arr[$i];
 }
 }
 return $maxVal;
 }
 catch (Exception $e)
 {
 echo $e->getMessage();
 }
}
function MaxArray2($arr)
{
 return MaxRecursive($arr,null);
}
Ashwin Parmar
3,0553 gold badges29 silver badges43 bronze badges
answered Jan 10, 2014 at 15:56
0
getMax($arr); 
function getMax($arr) { 
 foreach($arr as $arrr) { 
 echo max($arrr[2]); 
 }
}
empiric
7,8787 gold badges40 silver badges51 bronze badges
answered May 27, 2014 at 9:24
-2

You have to use recursion, with a single loop. For each element, if it is a number, we compare it with the current highest value, if it is an array we call the function recursively to check the array:

 function getHeighestNumber($array = array()) {
 static $number = 0;
 foreach ($array AS $key=>$value) {
 if(is_array($value)) { 
 $number = getHeighestNumber($value);
 } else if($value > $number) {
 $number = $value;
 } 
 }
 return $number;
 }
Tony BenBrahim
7,3602 gold badges39 silver badges51 bronze badges
answered Aug 8, 2014 at 11:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.