php - 我需要选择两个单词而不重复它们(PHP)

标签 php arrays select

我有一个数组列表,我想从此列表中选择两个单词而不重复它们。但是我尝试了很多东西,但没有任何效果。有当前代码:

$randomq2[] = "one";
$randomq2[] = "two";
$randomq2[] = "three";


srand ((double) microtime() * 1000000);
$getrando1 = rand(0,count($randomq2)-1);
$getrando2 = rand(0,count($randomq2)-1);


$wordone = $getrando1[$conco1];
$wordtwo = $getrando2[$conco2];

最佳答案

这应该适合你:

(在这里,我首先使用 array_unique()$randomq2 中获取所有唯一元素。然后我简单地使用 shuffle() 数组,最后我只使用 array_slice() 从开头提取 2 个元素。 )

<?php

    $randomq2 = array_unique($randomq2);
    shuffle($randomq2);
    $random = array_slice($randomq2, 0, 2);

    print_r($random);

?>

关于php - 我需要选择两个单词而不重复它们(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662527/

相关文章:

php - 如何在 PHP 中格式化日期而偏移量中没有冒号?

php - 动态 gettext 的 poedit 解决方法

php - 按事件 PHP 显示画廊

arrays - 二维 ColdFusion 结构

arrays - VBA 中的数组下标 - 谁能解释一下?

c++ - 使用 select() 函数改进服务器

php - php 中是否有类似 Java 的线程概念?

php - 将 MySQL 行与 PHP 数组值进行比较

mysql - 从 X.X+1、X+2、X+3、X+4 等返回每个第二窗口中记录的第一条记录

mysql - 与 mysql 比较的有趣结果