php - 检查多维数组中相同数组值的重复项?

标签 php arrays multidimensional-array

我正在尝试写一份圣诞马蹄铁购买 list 。 即:Steve 为 Jo 购买,Edward 为 Ally 购买等。 我有 19 个人一起工作。

目前我已经完成了两个数组。我使用 shuffle 来打乱原始名称,然后将它们加在一起以将它们存储到一个多维数组中。 它有效..但我有问题。

  1. shuffle 不关心这个人是否购买了自己的礼物。 (即:多维数组同值。即:[15] => Array ( [0] => Ben[1] => Ben)

  2. 我该如何为家庭成员破例,即:ben 不能为 Ellen、dave 和 chilli 买东西?等等。

我已经尝试查看各种数组方法,但我没有更多的答案。

我会这样想。

如果值相同,则重新洗牌并再次检查。 否则转到下一个值并再次检查。

<?php

$peoplearray = Array("ben","peter","oscar","jake", "heidi", "Steve", "ed", "matt", "jordan", "gilly" , "Lea", "ellen", "dave", "chilli", "Sean", "Mark", "shane", "ali","dean");

shuffle($peoplearray);
$buying_for = array();
$k=0;

foreach($peoplearray as $value){
print "<BR>";
$buying_for[$k] = $value;
$k++;
}

$peoplearray = Array("ben","peter","oscar","jake", "heidi", "Steve", "ed", "matt", "jordan", "gilly" , "Lea", "ellen", "dave", "chilli", "Sean", "Mark", "shane", "ali","dean");


$total = count($peoplearray);


$result = array();
foreach ($peoplearray as $i => $val) {
    $result[] = array($val, $buying_for[$i]);

}
  for ($row = 1; $row < $total; $row++) {
  echo "<p><b>Pair $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 2; $col++) {
    echo " ".$result[$row][$col]." ";
  }
  echo "</ul>";
}


?>

最佳答案

在将买家与收件人匹配时,这可以很好地混淆事物 - 不能完全确定这是否是您所追求的,但它可能有用。

    $people = array(
        'ben', 'peter', 'oscar', 'jake',
        'heidi', 'Steve', 'ed', 'matt',
        'jordan', 'gilly' , 'Lea', 'ellen', 
        'dave', 'chilli', 'Sean', 'Mark',
        'shane', 'ali', 'dean', 'bert', 
        'andrew', 'isabelle', 'laura', 'rebecca',
        'susan', 'huda', 'hazel', 'una',
        'isla', 'kerry', 'helen', 'thomas'
    );
    $matches=array();
    if( count( $people ) % 2 > 0 ) exit('There is an odd number of people - somebody would be missed.');

    function test($recipient,$buyer){
        return $recipient!==$buyer;
    }
    function implodearray( $array, $newline=false, $sep='=', $delim='&', $tabs=0 ){
        $tmp=array();
        if( is_array( $array ) && !empty( $array ) ){
            foreach( $array as $key => $value ) $tmp[]=$key.$sep.$value;
            $delimiter = $newline ? $delim . PHP_EOL : $delim;
            return implode( $delimiter . str_repeat( chr(9),$tabs ), $tmp );
        }
        return false;
    }

    foreach( $people as $index => $buyer ){
        $i = rand( 0, count( $people )-1 );
        while( $i==$index ) $i = rand( 0, count( $people )-1 );

        $matches[ ucfirst( strtolower( $buyer ) ) ]=ucfirst( strtolower( $people[ $i ] ) );
        array_splice( &$people, $i, 1 );
    }

    echo '<pre>',implodearray( $matches, 0,' buys for ', PHP_EOL),'</pre>';

关于php - 检查多维数组中相同数组值的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981436/

相关文章:

php - 一遍又一遍地设置 PHP 变量的更有效方法

php - 将多维数组的路径表示为字符串(例如表单输入名称数组)

php - mktime 在 6 月/7 月产生错误的奇怪行为

javascript - 奇怪的 ng-model 行为(ng-model 映射到数组元素)

javascript - 计算长字符串中每个句子的单词,jquery

java - NullPointerException:在数组和面板中添加 JButton

java - Android OpenGL 点数组

c++ - 交换函数不交换 C++ 中二维数组的元素

php - mysql 从表中选择第n列

php - PHP中简单数字的float到int改变值