可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have following array:
$ar3 = array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
I need to compare all of its elements with the help of recursion to find the maximum value.
I've managed to find the highest value in the deepest array:
$ar3 = array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
function arr_max_rec($ar3)
{
$max = $ar3[0];
foreach ($ar3 as $key => $value){
if ($max < $ar3[$key] and !is_array($value)){
$max = $ar3[$key];
}
elseif (is_array($ar3[$key])){
return arr_max_rec($ar3[$key]);
}
}return $max;
}
echo arr_max_rec($ar3);
But I need to compare all the numbers and find the highest one. The depth of the array can be any.
回答1:
You can try using recursive function
<?php
$ar3=array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
function highestValue($ar3) {
foreach($ar3 as $key => $value) {
if (is_array($value)) {
$ar3[$key] = highestValue($value);
}
}
return max($ar3);
}
echo highestValue($ar3); //1155
回答2:
The following would work:
<?php
function arr_max_rec($ar3)
{ // \
// |
// / reduce the input array to a single value using
// | |
// | | / the given callback
// | | |
return array_reduce($ar3, function ($a, $b) {
// /
// / return the -------
// | /
// | / max value of $a, $b. But if $b
// | |
// | | / is an array, recurse first
// | | | |
return max($a, is_array($b) ? arr_max_rec($b) : $b);
}, PHP_INT_MIN);
}
demo: https://3v4l.org/OUsmH
回答3:
PHP already offers a native recursive function to traverse all leaf nodes of a multidimensional array called array_walk_recursive(). This offers a clean, concise, intuitive line of code. When future developers look at your code, they will know immediately what your line of code is trying to achieve without following multiple lines of code and logic. This will enable better maintainability and show that you know a direct tool for the task at hand.
After establishing a base or default value for $max
, you write a user-defined function (or "closure") that handles the conditional logic. The fiddly thing about closures is that they have their own "scope" -- this means that the variables within are not available outside of the function (at least, not without help). global
declarations are generally inadvisable (in many cases a bad habit, and the line will be read on each iteration) even though they are well-intentioned, so use()
will serve as the means to import the variable and &
(which makes the variable "modifiable by reference") will serve as a means to export the variable from function scope back to the global scope.
Code: (Demo)
$ar3 = array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
$max = null; // declare a non-integer or minimum value here
array_walk_recursive($ar3, function($v)use(&$max){if($max === null || $v > $max) $max = $v;});
echo $max; // output: 1155
*array_walk_recursive()
returns true
or false
. You cannot use return
inside the closure to move $max
to the global scope.
*If you are going to initially declare $max
as 0
(because you don't expect negative values), then you won't need the $max === null
check in your conditional expression.
*If you are processing excessively deep multi-dimensional arrays (like hundreds of levels), then it is possible that a stack crash could occur. I tried to find a reference for this claim, but I can't find a link anywhere -- I did read it somewhere though.
回答4:
<?php
$ar3 = array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
$first = intval($ar3[0]);
$min = '' ;
$max = '' ;
foreach($ar3 as $data) {
$array= intval($data);
if($array<= $min ) {
$min = $array;
}
if($array> $max ) {
$max = $array;
}
}
echo " max = $max \n " ;
?>
Live Demo
回答5:
@Koiten below is your answer make correct your code according to below
one:
<?php
$ar3 = array(123, array(12, 665, array(77, 255, 98, 56), 8), 1155, 676);
function arr_max_rec($ar3){
$max = $ar3[0];
foreach($ar3 as $val){
if(!is_array($val) && $max < $val){
$max = $val;
}
else if(is_array($val)){
arr_max_rec($val);
}
}
return $max;
}
echo arr_max_rec($ar3);
?>
OUTPUT: 1155