the Internet Windows Android

PHP Find the maximum value in the array. PHP Get minimum and maximum values \u200b\u200bin a two-dimensional associative array

(PHP 4, PHP 5, PHP 7)

min - Finds the smallest value

Description

If only one is transmitted as arguments - an array of numbers, min () Returns the smallest of them. If the first argument is Integer or Float, then it must be at least one more. In this case, the function min () Return the smallest of them.

Comment:

Values \u200b\u200bof different types are compared using standard comparison rules. For example, not a numeric string ( string) will be compared with an integer ( integer) as if it is equal 0 , but several lines ( string) Compare alphabetically. Return value will save the initial type of variable, without conversion.

Return values

Function min () Returns the value of the parameter that is considered "the smallest" according to the standard comparison rules. If several values \u200b\u200bof different types are equal to each other (i.e. 0 and "ABC") The first one will be returned.

Examples

Example # 1 Example of Use min ()

echo min (2, 3, 1, 6, 7); // one
eCHO MIN (Array (2, 4, 5)); // 2.

// String "Hello", when compared with int, is considered as 0
// Since both values \u200b\u200bare equal, the order of parameters determines the result
echo min (0, "Hello"); // 0.
echo min ("Hello", 0); // Hello.

// Here we compare -1< 0, поэтому -1 является наименьшим значением
echo min ("Hello", - 1); // -one

// when comparing arrays of different lengths, min will return less long
$ Val \u003d min (Array (2, 2, 2), Array (1, 1, 1, 1)); // Array (2, 2, 2)

// Several arrays of the same length are compared from left to right
// For this example: 2 \u003d\u003d 2, but 4< 5
$ val \u003d min (Array (2, 4, 8), Array (2, 5, 1)); // Array (2, 4, 8)

// If an array and non-array are compared, then the array will never be returned
// Since arrays are considered large than all other values
$ val \u003d min ("String", Array (2, 5, 7), 42); // String

// If one argument is NULL or Boolean, it will compare with the rest
// Using the FALSE rule< TRUE, учитывая остальные типы аргументов
// in the example -10 and 10 are considered to be treated
$ val \u003d min (- 10, false, 10); // False
$ val \u003d min (- 10, , 10); // NULL.

// On the other hand, 0 is considered as false, so it is "less" than True
$ Val \u003d min (0, True); // 0.
?>

I have an array in this format:

Array (\u003d\u003e array (\u003d\u003e 117 \u003d\u003e networking \u003d\u003e 16) \u003d\u003e array (\u003d\u003e 188 \u003d\u003e ftp \u003d\u003e 23) \u003d\u003e array (\u003d\u003e 189 \u003d\u003e internet \u003d\u003e 48))

Is there a good way to get the minimum and maximum values \u200b\u200bof "Count"? I could do it using several cycles, but I thought it could be the best way.

Unlike others, you cannot use the MIN () / Max () functions for this problem, since these functions do not understand the data arrays transmitted in them (array). These functions operate only for scalar elements of the array.

Start image

The reason for which the use of min () and max () apparently gives the correct answer, belongs to the type-Casting arrays to the integers that are uncertain behavior:

Integer's conversion behavior is not defined for other types. Do not rely on any observed behavior, as it may change without warning.

My statement above about the type-casting was wrong. In fact, min () and max () work with arrays, but not how OP needs their work. When using min () and max () with several arrays or array of arrays, elements are compared to the element from left to right:

$ val \u003d min (Array (2, 4, 8), Array (2, 5, 1)); // Array (2, 4, 8) / * * First Element Compared to First Element: 2 \u003d\u003d 2 * Second Element Compared to Second Element: 4< 5 * first array is considered the min and is returned */

The OP translated into the problem shows why the direct use of min () and max () seems to give the correct result. The first elements of the array are id, so min () and max () first compare them, by the way, lead to the correct result, because the lowest ID is the one who has the lowest count and the highest ID is the one that has the highest possible Count.

End Edit.

The correct way is to use the cycle.

$ a \u003d array (array ("id" \u003d\u003e 117, "name" \u003d\u003e "Networking", "Count" \u003d\u003e 16), array ("id" \u003d\u003e 188, "name" \u003d\u003e "FTP", " Count "\u003d\u003e 23), array (" id "\u003d\u003e 189," name "\u003d\u003e" Internet "," Count "\u003d\u003e 48)); $ min \u003d php_int_max; $ Max \u003d 0; Foreach ($ A AS $ i) ($ min \u003d min ($ min, $ i ["Count"]); $ max \u003d max ($ max, $ i ["Count"]);)

You can use the MAX () and MIN () functions.

What did you do with several cycles? One is quite enough 🙂

  1. Get the first element by calculating the account of both $ min and MAX
  2. iterations for the rest, compare the meter with each $ min and $ max, if less / more, assign a new account value

You can use the MAX / MIN functions as they return an array containing maximum / minimum of each index. Your example should return Array (189, "Networking", 48) for Max. Then you can capture the account from this array.

Update this does not work, as I excluded. An example of a reference page is misleading, and the example gives the correct result using MAX, but it is just a coincidence.

Looks like you can not use max () for a 2D array. It simply returns the largest array, not the max () of each index (as indicated in several answers).

$ Count \u003d Array (); foreach ($ arr as $ _Arr) ($ Count \u003d $ _Arr ["Count"];) VAR_DUMP (MAX ($ Count), Min ($ Count));

There is equivalent built-in Function for this? (even without the possibility testing)

/ ** * Extracts a column from a 2D array with an optional choice over another column * * * @Param $ an ARRAY array to extract from * @param $ acolname column name for extraction, for example. "O_NAME" * @Param $ acoltest (optional) column name to perform a test, for example. "O_ID" * @Param $ ATEST (optional) string for test EX. "\u003e \u003d 10", "\u003d\u003d". "$ Toto." "" * @Return 1d An array with only the extracted column * @Access Public * / Function ExtractColfromaMarray ($ Aarray, $ acolname, $ acoltest \u003d "", $ ATEST \u003d "") ($ MRES \u003d Array (); Foreach ($ aarray as $ ROW) (if (($ acoltest \u003d\u003d ") || (EVAL (" Return ". $ ROW [$ acoltest]. $ ATEST. ";"))) ($ MRES \u003d $ ROW [$ acolname];)) Return $ Mres;) // ExtractColfromarray

Arrays are one of the convenient structured storage methods of information. Each element of such an array has its own place, its own key and value. The content of the arrays may be different, as, for example, the base of numbers, names, or simple numeric values. Speaking about the number, we can get up to all sorts of tasks, for example, the output of the maximum or minimum value. About how this is solved in different programming languages, today and will be discussed.

We find the largest and smallest value of a one-dimensional array on PHP

All arrays differ in their structure. Consider two simple one-dimensional array, one of which does not contain keys:

$ MY_ARRAY \u003d Array (22, 24, 37, 74, 23, 2, 10);

and one identical to the previous one, but already with keys:

$ MY_ARRAY \u003d Array (1 \u003d\u003e 22, 2 \u003d\u003e 24, 3 \u003d\u003e 37, 4 \u003d\u003e 74, 5 \u003d\u003e 23, 6 \u003d\u003e 2, 7 \u003d\u003e 10);

We will try to remove the maximum and minimum value of this array. To do this, we will use standard functions " max"And" mIN."Respectively:

Echo Max ($ My_Array); // withdraw 74 echo min ($ my_array); // withdraw 2.

If we consider in more detail the second array, then as a result we can get the key of the maximum or minimum values.

On the example of the array

$ MY_ARRAY \u003d Array (1 \u003d\u003e 22, 2 \u003d\u003e 24, 3 \u003d\u003e 37, 4 \u003d\u003e 74, 5 \u003d\u003e 23, 6 \u003d\u003e 2, 7 \u003d\u003e 10);

it will look like this:

$ max \u003d array_keys ($ My_array, Max ($ My_array)); $ max \u003d $ max; // Maximum value of $ min \u003d Array_Keys ($ My_array, Min ($ My_array)); $ min \u003d $ min; // key of the minimum value of Echo $ MAX; // withdraw the result of the maximum value

Accordingly, the key of the maximum value is "4", and the minimum - "6".

We find the greatest and smallest importance of the multidimensional array on PHP

Multidimensional arrays are characterized by their nesting. For example, a two-dimensional array will look as follows without keys:

$ My_Array \u003d Array (Array (22, 24, 37), Array (74, 23, 2), Array (10));

And, accordingly, with some keys:

$ My_Array \u003d Array (Array (1 \u003d\u003e 22, 2 \u003d\u003e 24, 3 \u003d\u003e 37), Array (4 \u003d\u003e 74, 5 \u003d\u003e 23, 6 \u003d\u003e 2), Array (7 \u003d\u003e 10));

In this case, finding the maximum and minimum values \u200b\u200bis a bit difficult, but also really.

To begin with, to find a maximum and minimum here, we transform an array to one-dimensional:

$ Out_array \u003d Array (); Foreach ($ My_Array as $ sub_array) ($ Out_array \u003d Array_Merge ($ Out_array, $ sub_array);)

The design works for both options above. And further, following the example of a one-dimensional array, withdraw the data we need:

Echo Max ($ Out_array); // withdraw 74 echo min ($ out_array); // withdraw 2.

As a small bonus, I will give an example of another popular two-dimensional array:

$ My_Array \u003d Array (Array ("id" \u003d\u003e "1", "date" \u003d\u003e "2018-03-19", "Price" \u003d\u003e "5",), array ("id" \u003d\u003e "2" , "Date" \u003d\u003e "2018-03-19", "price" \u003d\u003e "50",), array ("id" \u003d\u003e "3", "date" \u003d\u003e "2018-03-19", " Price "\u003d\u003e" 25 ",));

Before popularity, I mean not the content, but an example of its structure. Suppose that here you need to make the output of the maximum and minimum value only key "Price".

The first thing you need in this case is to get a new array only with these data:

$ Numbers \u003d Array_Column ($ My_Array, "Price");

Echo min ($ numbers); // withdraw 5 ECHO MAX ($ numbers); // withdraw 50.

On this, work with PHP arrays is finished. If suddenly the structure of your array is different and you do not know how to process it - ask the appropriate question in the comments, I will try to help you.

We find the greatest and smallest value of a one-dimensional array on JavaScript

Unlike PHP, in JavaScript, the type of arrays is much easier, and a simple one-dimensional array will look like this:

Var my_array \u003d;

Indexes here are not specified. In order to find the maximum and minimum value in this array, write two of your simple features:

FUNCTION ARRAYMAX (RETURN ARRAY.REDUCE (RETURN MATH.MAX (A, B);));) Function Arraymin (Array) (Return Array.Reduce (Function (A, B) ( Return Math.min (A, B);));)

which are used to find the values \u200b\u200bwe need. Use is also simple:

Alert (Arraymax (My_Array)); // withdraw 74 Alert (Arraymin (My_Array)); // withdraw 2.

In this case, the number "2" and "74" as the minimum and maximum value of the array will be displayed on the screen.

We find the greatest and smallest meaning of the multidimensional array on JavaScript

Multidimensional arrays in JavaScript are as simple, and they look like this:

Var my_array \u003d [,,];

Let's try to find a maximum and minimum here. To begin with, we will write a function with which the scheme is usually introducing this array as a familiar scheme as one-dimensional:

Var out_array \u003d; My_array.Foreach (Function (V) (Array.Prototype.push.apply (Out_array, V);));

And with the help of the object " Math."We get the values \u200b\u200bwe need:

Var min \u003d math.min.apply (NULL, OUT_ARRAY); // We obtain 2 var max \u003d math.max.apply (NULL, OUT_ARRAY); // Receive 74 Alert (MAX); // withdraw 74 on the screen

In fact, instead of the object " Math."You can use our functions used in an option with a one-dimensional array, but that you understand that any task can be solved in several ways - here I brought a little different solution.

Well, according to tradition, a small bonus. Consider another multi-dimensional array with such a structure:

Var my_array \u003d [["one", "2018-03-19", 5], ["two", "2018-03-19", 50], ["three", "2018-03-19", 25 ],];

As we see, numeric values \u200b\u200bin each array are in third place. Let's write the code and obtain the appropriate values \u200b\u200bof only this data:

Var min \u003d + infinity; var max \u003d -infinity; My_Array.Foreach (Function (Item) (IF (+ Item< min) { min =+ item; // Ищем минимальное значение } }); my_array.forEach(function(item) { if(+item > Max) (max \u003d + item; // We are looking for the maximum value))); alert (min + "" + max); // Display the result on the screen

That's all. Do not forget to support the project. Ahead of you awaits many interesting things!