Get maximum value from multidimensional array

Here is my multidimensional array:

$arrOrg = [2, 3, [5, 7, 1], 100, [6, 9, [14, 95]], 78];

I want to get the maximum value from this array.

Here is what I have tried so far:

$highest = 0;
function getHighest($arr) {
    for ($i = 0; $i < count($arr); $i++) {
        if (is_array($arr[$i])) {
            getHighest($arr[$i]);
        } else {
            if ($arr[$i] > $arr[$i + 1]) {
                $highest = $arr[$i];
            } else {
                $highest = $arr[$i + 1];
            }
        }
    }
    return $highest;
}
echo getHighest($arrOrg);

But it gives the wrong result: 78

You can help me?

+4
source share
6 answers

Maybe something like this:

$arrOrg = [2, 3, [5, 7, 1], 100, [6, 9, [14, 95]], 78];
$json   = json_encode($arrOrg);
$json   = str_replace(array('[', ']'), "", $json);
$arr    = explode(",", $json);

echo $maximum = max($arr);

EDIT after comment by Vahe Galstyan

$arrOrg = array(
    2, 
    3,
    array(5, 7, 1), 
    100,
    array(
        6, 
        9, 
        array(14, 95)
    ), 
    78
);

$json = json_encode($arrOrg);
$json = str_replace(array('[', ']'), "", $json);
$arr  = explode(",", $json);

echo $maximum = max($arr);
+6
source

You can use this function to do this.

  function getHighest($array) {
       foreach($array as $key => $value) {
           if (is_array($value)) {
               $array[$key] = getHighest($value);
           }
       }

       sort($array);

       return array_pop($array);
    }

    echo getHighest($arrOrg);
+2
source

, json,... remove "[" and "]", max. ... php . . , ^ _ ^.

.

$highest = null;

array_walk_recursive($arrOrg, function($item, $key) use (&$highest) {
    if (!isset($highest) || $highest < $item) {
        $highest = $item;
    }
});

echo $highest;
+1

you can also achieve this with array_walk_recursive () and max () as shown below to output the output here https://eval.in/809971

<?php
   $arrOrg = [2, 3, [5, 7, 1], 100, [6, 9, [14, 95]], 78];
   $oneD = array();
   array_walk_recursive($arrOrg,function ($a) use (&$oneD){
        $oneD[] = $a;

   });
   echo "Maximum value = ".max($oneD);
+1
source

You can do this using this method.

<?php

$array = [2, 3, [5, 7, 1], 100, [6, 9, [14, 95]], 78];
$highest_value=0;
get_highest_value($array);
function get_highest_value($array){
    foreach($array as $val){
        if(is_array($val)){
            get_highest_value($val);
        }else{
            if($GLOBALS['highest_value'] < $val){
                $GLOBALS['highest_value'] = $val;
            }
        }
    }
}
echo $highest_value; // output 100
0
source

Without globals, changing the array or doing additional conversions:

funcion getMax ($arr) {
    $max = null;
    foreach ($arr as $act) {
        if (is_array ($act)) {
            $act = getMax ($act);
        }
        if ($max == null || act > $max) {
            $max = $act;
        }
    }
    return $max;
}
0
source

Source: https://habr.com/ru/post/1678383/


All Articles