我选择了两个用户ID。
例如:
$user_one = 1034;
$user_two = 1098;
$new_id = "";
现在我想选择随机这两个用户中的一个。有人可以帮我如何从给定的ID中选择随机ID。
例如:
$new_id = $get_random_id //Must be 1034 or 1098
答案 0 :(得分:6)
像这样:
$new_id = rand(0, 1) ? $user_one : $user_two;
rand(0,1)
随机提供0或1,评估为true
或false
以随机选择一个或另一个三元结果。
答案 1 :(得分:4)
看到这个问题被误解,这里有一个关于如何从两者中获取随机ID的更新答案:
将它们放入数组并使用array_rand()
。
$user_one = 1034;
$user_two = 1098;
$ids = array();
$ids[] = $user_one;
$ids[] = $user_two;
$new_id = $ids[array_rand($ids)];
答案 2 :(得分:1)
如果您必须从给定用户中选择一个用户(而不是用户之间)
// create an array with all the users ( assuming if the users are more than 2)
$user_array = array($user_one,$user_two);
// get a random key from array
$random_key = array_rand($user_array, 1);
// get value of random key
$new_id = $user_array[$random_key];