How to Get Max and Min Value in a Multidimensional Array

Find min/max in a two dimensional array

Here's one way to get the min and max values:

$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));

To return the nested array for the min and max:

$prices = array_column($array, 'Price');
$min_array = $array[array_search(min($prices), $prices)];
$max_array = $array[array_search(max($prices), $prices)];

You could do each in one line since that looked like what you were trying to do:

$min_array = $array[array_search(min($prices = array_column($array, 'Price')), $prices)];
$max_array = $array[array_search(max($prices = array_column($array, 'Price')), $prices)];

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column().

Using array_map() to get just the min and max:

$min = min(array_map(function($a) { return $a['Price']; }, $array));
$max = max(array_map(function($a) { return $a['Price']; }, $array));

There's probably a good array_filter() or array_reduce() as well.

min and max in multidimensional-array

I thinik you will have to write your own function:

<?php  
function max_with_key($array, $key) {
if (!is_array($array) || count($array) == 0) return false;
$max = $array[0][$key];
foreach($array as $a) {
if($a[$key] > $max) {
$max = $a[$key];
}
}
return $max;
}


$dataPoints = array(
array('x' => 2343, 'y' => 4322),
array('x' => 103, 'y' => 303 ),
array('x' => 2345,'y' => 2321 ),
array('x' => 310, 'y' => 2044 ),
array('x' => 173, 'y' => 793 ),
array('x' => 456, 'y' => 2675),
array('x' => 24, 'y' => 819 ));

$max_x = max_with_key($dataPoints, 'x'); //2343
$max_y = max_with_key($dataPoints, 'y'); //4322
?>

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);

Finding minimum and maximum in Java 2D array

Ok, I've kinda fixed your code. Actually your mistake was that you have not been traversing all the cells of your multidimensional array.

So, I've added additional loop into getMinValue/getMinValue methods and fixed array elements addressing.

import java.util.*;

class MinMax {
public static void main(String[] args) {
int[][] data = {
{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8}
};
System.out.println(getMaxValue(data));
System.out.println(getMinValue(data));
}


public static int getMaxValue(int[][] numbers) {
int maxValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] > maxValue) {
maxValue = numbers[j][i];
}
}
}
return maxValue;
}

public static int getMinValue(int[][] numbers) {
int minValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] < minValue ) {
minValue = numbers[j][i];
}
}
}
return minValue ;
}
}

Find nested min/max array in a two dimensional array

The output you wanted is strange, but this does the trick.

const arr = [[1622306648284, 1.4025036293793085],[1622309604992, 1.384071162873584],[1622312530257, 1.3503030062861177],[1622315654724, 1.3625441847416848],[1622318703104, 1.3645747739529213],[1622321575558, 1.3611235799170127],[1622324539379, 1.3750838657128996],[1622327549644, 1.378768535066251],[1622330652746, 1.3916061750979443],[1622333538315, 1.4076792700030256],[1622336466156, 1.3674852893896725]];

const max = arr.reduce((a, b) => a[1] >= b[1] ? a : b);
const min = arr.reduce((a, b) => a[1] <= b[1] ? a : b);

console.log(`Min: ${min} Max: ${max}`);

php multidimensional array - how to find min value and return key

Feels like I'm solving your homework question:

$min = PHP_INT_MAX;
$idx = null;
for($i=0; $i < count($r); $i++)
{
if($min > $r[$i]['price'])
{
$min = $r[$i]['price'];
$idx = $i;
}
}

if($idx != null) {
echo "Free Shipping". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
}

Answer to your edit.

<?php
$r = array (
'DOM.EP' => array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
'DOM.PC' => array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
'DOM.IS' => array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
'DOM.SM' => array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10)
);

$min = PHP_INT_MAX;
$idx = null;
foreach ($r as $key => $value) {
if($min > $r[$key]['price'])
{
$min = $r[$key]['price'];
$idx = $key;
}
}

if($idx != null) {
echo "Free Shipping ". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
}
?>

Find minimum and maximum value from two dimensional array

void fillUpArray(int newArray[5][5])
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
int randomNumber = rand() % 100 + 1;
printf("Random number[%d][%d]: %d\n", i, j, randomNumber);
newArray[i][j] = randomNumber;
}
}
}

void printMinimumMaximum(int myArray[5][5])
{
int minimum = myArray[0][0];
int maximum = myArray[0][0];

for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
if (myArray[i][j] < minimum)
{
minimum = myArray[i][j];
}
if (myArray[i][j] > maximum)
{
maximum = myArray[i][j];
}
}
}

printf("Minimum: %d\n", minimum);
printf("Maximum: %d\n", maximum);
}

int main()
{
int minMax[5][5];
fillUpArray(minMax);
printMinimumMaximum(minMax);
return 0;
}

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


Related Topics



Leave a reply



Submit