获取数组中的最小值或随机值是否都相同? [英] Getting lowest value in array or random value if all is the same?

查看:71
本文介绍了获取数组中的最小值或随机值是否都相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组

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

键5和键6是用户ID.两个键的值0是它们所拥有的帖子数.

the key 5 and key 6 are user id's. the value 0 for both the keys are the number of posts they have.

我如何获取帖子数最少的用户,如果有1个以上相同/最低帖子的用户,请随机选择一个.

How do i obtain the user with the lowest post and if there are more than 1 user with the same/lowest post, select one by random.

谢谢

推荐答案

假设您的数组为$arr

$mini = min($arr);
$user = array();
foreach ($arr as $key => $val){
    if ($val == $mini){
        // find the user with minimum value
        $user[] = $key;
    }
}
// print the user with minimum value
echo array_rand($user, 1).' '.$mini;

这篇关于获取数组中的最小值或随机值是否都相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆