简体   繁体   中英

PHP Arrays : How to Find the Lowest/Highest Value

I have two array like this :

$array1 = array(1,1,2,3,3,4,5); //remember that i have two '1' value in this array
$array2 = array($url1, $url2, $url3, $url4, $url5, $url6);

I wish to find the lowest/highest value in $array1 then link to $url1/$url5 like this :

<a href="$url1">1</a> or <a href="$url6">5</a>

How I can make this happen using PHP? Any help would be greatly appreciated Thanks

Use the max() & min() function

max — Find highest value
min — Find lowest value

Example code:

$max = max($array);
$min = min($array);

If the array is already sorted, use $array2[0] and $array2[count($array2)-1] .

If it's not already sorted, you can use this to sort the arrays.

array_multisort($array1, SORT_NUMERIC, $array2);
$lowest = $array2[0];
$highest = $array2[count($array2)-1];

据我了解,您需要这两个功能: maxmin

try this

$maxValueKeys = array_keys($array1, max($array1)); // Your min value indexes
$minValueKeys = array_keys($array1, min($array1)); // Your max value indexes

But it isn't an associative array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM