我需要一些帮助/指导来设置 PHP 脚本以随机配对数组中的项目。
我一直在玩这篇文章中的第二个脚本 https://stackoverflow.com/questions/3758223/using-php-randomly-pair-up-group-of-items-not-pairing-any-with-itself-no-direc但是,我却没有取得任何进展。任何帮助表示赞赏。
非常简单的方法,但希望对您有所帮助:
(mates,如果分组在数组中(例如数组('a1','a2')),不会配对。)
function matchUp($array) {
$result = array();
while($el = array_pop($array)) {
shuffle($array);
if (sizeof($array) > 0) {
$candidate = array_pop($array);
$result[] = array(
array_pop($el),
array_pop($candidate)
);
if (sizeof($el) > 0) {
$array[] = $el;
}
if (sizeof($candidate) > 0) {
$array[] = $candidate;
}
}
else {
$result[] = array(array_pop($el));
}
}
return $result;
}
$array = array(
array('a1', 'a2'),
array('b1', 'b2'),
array('c1'),
array('d1'),
array('e1', 'e2'),
array('f1'),
array('g1', 'g2'),
);
Update:
foreach(matchUp($array) as $pair) {
list($a, $b) = $pair + array(null, null);
echo '<div style="border: solid 1px #000000;">' . $a . ' + ' . $b . '</div>';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)