Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the minimum and maximum values in an array column

I have an array in this format:

$array = [
    ['id' => 117, 'name' => 'Networking', 'count' => 16],
    ['id' => 188, 'name' => 'FTP', 'count' => 23],
    ['id' => 189, 'name' => 'Internet', 'count' => 48],
];

Is there a good way to retrieve the minimum and maximum values of the 'count' column (16 and 48 respectively)?

I could do this using a few loops, but I wonder if there may be a better way.

like image 621
James B Avatar asked Aug 28 '09 06:08

James B


People also ask

How do you find the maximum and minimum of an array in C++?

This problem can also be solved using the inbuild functions that are provided in the standard template library of the C++ programming language. The methods to find the solution are min_element() and max_element() and these methods are found in the bits/stdc++. h library in C++.

How do you find the max and min value of an array in Python?

In python is very easy to find out maximum, minimum element and their position also. Python provides different inbuilt function. min() is used for find out minimum value in an array, max() is used for find out maximum value in an array. index() is used for finding the index of the element.


1 Answers

In contrast to what others have posted, you cannot use the min()/max() functions for this problem as these functions do not understand the datastructure (array) which are passed in. These functions only work for scalar array elements.


BEGIN EDIT

The reason why the use of min() and max() seem to yield the correct answer is related to type-casting arrays to integers which is an undefined behaviour:

The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

My statement above about the type-casting was wrong. Actually min() and max() do work with arrays but not in the way the OP needs them to work. When using min() and max() with multiple arrays or an array of arrays elements are compared element by element from left to right:

$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)
/*
 * first element compared to first element: 2 == 2
 * second element compared to second element: 4 < 5
 * first array is considered the min and is returned
 */

Translated into the OP's problem this shows the reason why the direct use of min() and max() seems to yield the correct result. The arrays' first elements are the id-values, therefore min() and max() will compare them first, incidentally resulting in the correct result because the lowest id is the one with the lowest count and the highest id is the one with the highest count.

END EDIT


The correct way would be to use a loop.

$a = array(
        array('id' => 117, 'name' => 'Networking', 'count' => 16),
        array('id' => 188, 'name' => 'FTP', 'count' => 23),
        array('id' => 189, 'name' => 'Internet', 'count' => 48)
);
$min = PHP_INT_MAX;
$max = 0;
foreach ($a as $i) {
    $min = min($min, $i['count']);
    $max = max($max, $i['count']);
}
like image 145
Stefan Gehrig Avatar answered Oct 05 '22 06:10

Stefan Gehrig