Getting lowest value in array or random value if all is the same?

后端 未结 2 2055
半阙折子戏
半阙折子戏 2021-01-26 10:41

I have an array like so

Array
(
    [5] => 0
    [6] => 0
)

the key 5 and key 6 are user id\'s. the value 0 for both the keys are the num

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 11:23

    What you need is min() for the lowest value in the array and array_rand() to get a random entry out of the array.

    $yourArr = array(4, 4, 3, 5);
    
    $lowestEntry = min($yourArr);
    $duplicateEntries = array_keys($yourArr, $lowestEntry);
    
    echo (count($duplicateEntries) > 1)?$yourArr[array_rand($duplicateEntries, 1)]:$lowestEntry;
    

提交回复
热议问题