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);
}
7 Answers 7
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
-
thank you, the best logic i've seen. I didn't now if we can dow self method inside the function.GusDeCooL– GusDeCooL2011年11月10日 21:33:15 +00:00Commented Nov 10, 2011 at 21:33
-
2Calling 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.dimme– dimme2011年11月10日 21:49:23 +00:00Commented Nov 10, 2011 at 21:49
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;
-
2What if the value are all < -1 ?Aurelio De Rosa– Aurelio De Rosa2011年11月10日 21:24:26 +00:00Commented Nov 10, 2011 at 21:24
-
I'd say: set $max to null at beginning, change conditional to (is_null($max) || $key > $max)Steve Lewis– Steve Lewis2011年11月10日 21:26:14 +00:00Commented Nov 10, 2011 at 21:26
-
1@Steve Or you can set $max to -INFPradeep– Pradeep2011年11月10日 21:44:12 +00:00Commented Nov 10, 2011 at 21:44
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;
}
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
}
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);
}
getMax($arr);
function getMax($arr) {
foreach($arr as $arrr) {
echo max($arrr[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;
}
is_array()
: php.net/manual/en/function.is-array.phpWrite a recursive function which calls itself
like as you said.