如果数组值彼此匹配,那么再次洗牌

时间:2022-02-04 13:05:11

I have a form where users can input numbers in 7 different fields and then I use

我有一个表单,用户可以在7个不同的字段输入数字,然后使用。

shuffle($_POST);

to mix up the numbers and then display them against the numbers they in-putted originally, so the output looks like this:

将这些数字混合,然后根据它们最初输入的数字进行显示,因此输出如下:

1 1
2 4
3 6
4 2
5 7
6 3
7 5

Here is the code I'm using.

这是我正在使用的代码。

<?php 
error_reporting(0);
if ($_SERVER['REQUEST_METHOD']== "POST") {
$array = implode("",$_POST);
shuffle($_POST);
}
?>
<div class="box"><?php echo $array[0]; ?> <?php echo $_POST[0]; ?></div><br>
<div class="box"><?php echo $array[1]; ?> <?php echo $_POST[1]; ?></div><br>
<div class="box"><?php echo $array[2]; ?> <?php echo $_POST[2]; ?></div><br>
<div class="box"><?php echo $array[3]; ?> <?php echo $_POST[3]; ?></div><br>
<div class="box"><?php echo $array[4]; ?> <?php echo $_POST[4]; ?></div><br>
<div class="box"><?php echo $array[5]; ?> <?php echo $_POST[5]; ?></div><br>
<div class="box"><?php echo $array[6]; ?> <?php echo $_POST[6]; ?></div><br>

How do I check two arrays values against each other to make sure they don't match. If they do match then I would like to use shuffle() again untill not one of them match. I will probably use an if else statement to run the code again until the numbers don't match but not sure how to go about it.

如何检查两个数组的值,以确保它们不匹配。如果它们匹配,那么我希望再次使用shuffle(),直到它们中没有一个匹配。我可能会使用if else语句再次运行代码,直到数字不匹配,但不知道该如何进行。

3 个解决方案

#1


3  

Check array_intersect_assoc($array,$_POST); in while loop until it returns null array, shuffle!

首先检查(数组,美元$ _POST);在while循环中,直到返回空数组,洗牌!

using array_intersect_assoc():

使用首先对():

$array = $_POST;
do {
    shuffle($_POST);

} while ((count(array_intersect_assoc($array, $_POST))) !=0);

#2


1  

You could write a helper function to test if the arrays have matches, and shuffle until there are no matches left.

您可以编写一个助手函数来测试数组是否有匹配项,然后拖放直到没有匹配项。

function has_same_matches($arr1, $arr2) {
    for($i=0, $sz = count($arr1); $i < $sz; ++$i) {
        if ($arr1[$i] == $arr2[$i]) return true;
    }
    return false;
}

$array = [1,2,3,4,5,6,7];
$shuffled = $array;

while(has_same_matches($array, $shuffled)) {
    shuffle($shuffled);
}

#3


1  

I'm not sure why you'd want to use $_POST like that directly, but anyway I think this soultion should work for you. Also please note that, shuffle() does not preserve the array keys.

我不知道为什么你想直接使用$_POST,但是无论如何我认为这个灵魂应该适合你。还请注意,shuffle()并不保存数组键。

if ($_SERVER['REQUEST_METHOD']== "POST") {
    $init_array = $_POST;
    $array = implode("",$_POST); //user input
    shuffle($_POST);
    $after_shuffle = $_POST;
    while($init_array == $after_shuffle){
       shuffle($_POST);
    }
    $final_array = $_POST; //array with unmatched values
 }

#1


3  

Check array_intersect_assoc($array,$_POST); in while loop until it returns null array, shuffle!

首先检查(数组,美元$ _POST);在while循环中,直到返回空数组,洗牌!

using array_intersect_assoc():

使用首先对():

$array = $_POST;
do {
    shuffle($_POST);

} while ((count(array_intersect_assoc($array, $_POST))) !=0);

#2


1  

You could write a helper function to test if the arrays have matches, and shuffle until there are no matches left.

您可以编写一个助手函数来测试数组是否有匹配项,然后拖放直到没有匹配项。

function has_same_matches($arr1, $arr2) {
    for($i=0, $sz = count($arr1); $i < $sz; ++$i) {
        if ($arr1[$i] == $arr2[$i]) return true;
    }
    return false;
}

$array = [1,2,3,4,5,6,7];
$shuffled = $array;

while(has_same_matches($array, $shuffled)) {
    shuffle($shuffled);
}

#3


1  

I'm not sure why you'd want to use $_POST like that directly, but anyway I think this soultion should work for you. Also please note that, shuffle() does not preserve the array keys.

我不知道为什么你想直接使用$_POST,但是无论如何我认为这个灵魂应该适合你。还请注意,shuffle()并不保存数组键。

if ($_SERVER['REQUEST_METHOD']== "POST") {
    $init_array = $_POST;
    $array = implode("",$_POST); //user input
    shuffle($_POST);
    $after_shuffle = $_POST;
    while($init_array == $after_shuffle){
       shuffle($_POST);
    }
    $final_array = $_POST; //array with unmatched values
 }