Get the Maximum Value from an Element in a Multidimensional Array

Find highest value in multidimensional array

Since PHP 5.5 you can use array_column to get an array of values for specific key, and max it.

max(array_column($array, 'Total'))

How to find the highest value in a multidimensional array?

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

find max() of specific multidimensional array value in php

Think array_reduce if you want to compute some single value iteratively over an array:

$max = array_reduce($array, function($a, $b) { 
return $a > $b['time'] ? $a : $b['time'];
} );

Or you could make use of a utility function like:

function array_col(array $a, $x)
{
return array_map(function($a) use ($x) { return $a[$x]; }, $a);
}

which would be used like:

$max = max(array_col($array, 'time'));

It's more general purpose and could be used for other similar things.

Get the maximum value from an element in a multidimensional array?

$max = 0;
foreach($array as $obj)
{
if($obj->dnum > $max)
{
$max = $obj->dnum;
}
}

That function would work correctly if your highest number is not negative (negatives, empty arrays, and 0s will return the max as 0).

Because you are using an object, which can have custom properties/structures, I don't believe there are really any 'predefined' functions you can use to get it. Might as well just use a foreach loop.

You really can't get away from a foreach loop, as even internal functions use a foreach loop, it is just behind the scenes.

Another solution is

$numbers = array();
foreach($array as $obj)
{
$numbers[] = $obj->dnum;
}
$max = max($numbers);

Finding the Max value in a two dimensional Array

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers)) # max of those max-numbers
4

Max value of a multidimensional array javascript

let

var arr = [[2,3], [4,5]]; // a multidimensional array

then get an array with each row's maximum with

var maxRow = arr.map(function(row){ return Math.max.apply(Math, row); });

and the overal maximum with

var max = Math.max.apply(null, maxRow);

Return Indices Of Maximum Value in a Multidimensional Array in R

This provides the answer I was looking for in the format of [x1, x2, x3] where x2=20:

which( test==max(test[,20,],na.rm=T) , arr.ind = T )

If the indices in the matrix are wanted without constraining it to a 2D by setting x2=20, you can use the below line:

which( test==max(test,na.rm=T) , arr.ind = T )

If there are multiple indices sets that have the same maximum value, you can select the first set from the list with

which( test==max(test,na.rm=T) , arr.ind = T )[1,]

How to find maximum value in whole 2D array with indices

Refer to this answer, which also elaborates how to find the max value and its (1D) index, you can use argmax()

>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3

You can then use unravel_index(a.argmax(), a.shape) to get the indices as a tuple:

>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)


Related Topics



Leave a reply



Submit