Php 从数组创建一个数组并保持相同的顺序
我制作了一副非常简单的卡片。我使用3个数组,$deck,$cards,&$used$牌组是场上的牌,$牌是比较数组,$使用是丢弃数组。当从$deck使用卡时,它们将在$used数组中占有一席之地。我的问题是,当它们被放置在$used数组中时,它们会按数字顺序排列。我不要这个。我需要它们的顺序与从$deck数组中移除它们的顺序相同。完整代码如下Php 从数组创建一个数组并保持相同的顺序,php,arrays,shuffle,Php,Arrays,Shuffle,我制作了一副非常简单的卡片。我使用3个数组,$deck,$cards,&$used$牌组是场上的牌,$牌是比较数组,$使用是丢弃数组。当从$deck使用卡时,它们将在$used数组中占有一席之地。我的问题是,当它们被放置在$used数组中时,它们会按数字顺序排列。我不要这个。我需要它们的顺序与从$deck数组中移除它们的顺序相同。完整代码如下 <?php /* This code creates a deck of cards and deals them It also
<?php
/*
This code creates a deck of cards and deals them
It also creates a discard pile of used cards. It never
deals duplicates and will re-shuffle the deck if it
gets under 7 cards.
*/
dealCards();
/*
This function creates the deck of cards.
It will deal out 5 cards and remove those
5 cards from the deck.
*/
function dealCards() {
// checks to see if a session has been created
if (isset($_SESSION["deck"])) {
$deck = $_SESSION["deck"];
} else {
// if session has not been created it
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
// comparison array.
$cards = range(1, 52);
// will use the same deck until it has 7 or less cards
if (count($deck) > 7) {
// deals 5 cards from the array $deck
for ($i = 0; $i < 5; $i++) {
print "<img src="/main/deck/$deck[$i].png" alt="Card: $deck[$i]" />";
// removes dealt cards from the deck
unset($deck[$i]);
// clears empty values from the array
$deck = array_values(array_filter($deck));
// replaces session data with new deck
$_SESSION["deck"] = $deck;
}
} else {
// shuffle the full 52 cards again if the deck is less than 7 cards
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
/*
compares $deck to the comparison array $cards
and places the differences in an array
called $used which then can be used as a discard
pile.
*/
$used = array_diff($cards, $deck);
// testing purposes.
print "<pre>";
print_r ($deck);
print "</pre>";
print "<pre>";
print_r ($used);
print "</pre>";
}
?>
我想到的第一件事是,当你解除你的甲板阵列 您可以将卡放入$used数组中 但这只是第一次奏效。在第二次使用dealCards时,它将被覆盖。 另外,$used数组中肯定会有一些空位置。
你需要永久保存吗?我已经让它工作了。代码需要清理一下,但至少可以工作。谢谢瑞肯和其他人的帮助。下面是修改后的工作代码
如果需要将它们按相同顺序排列,请将数组也存储在会话中。在未设置$deck[$i];,执行$_会话['used'][=$deck[$i];。如果没有存储卡片使用顺序的地方,您永远无法以相同的顺序“重建”该精确数组。我尝试了您的建议,但它们仍然以数字顺序填充$used数组。然后,或者您正在使用它执行其他操作,从而导致重新排序。开箱即用,它的工作原理如链接代码所示。我让它工作了,但是它在$used数组中只保存了5张卡。每次交易它都会用新发的5张牌替换之前的5张牌。我试图将每一张牌都保留在$used中,直到牌组重新洗牌。您正在覆盖某处的$u会话['used']。这可能有两个主要原因:1一个逻辑错误,它确实在某处重置了$_会话['used'],2您可能已经启用了它,它在PHP 5.3.0中被弃用,在PHP 5.4.0中被删除。你在某处使用了一个$used变量。我只需要将它们删除并存储,直到$deck数组有7个或更少的值,在这一点上,整个函数都会重置。让我直截了当地说,如果你有这种1,4,6,9级丢弃的卡,$used数组应该是什么样子?假设前5张卡是95276,第二组5张卡是108431。生成$used数组时,它会按1 2 3 4 5的顺序下降。它们应按处理顺序存储。从2个手示例中,$used数组应该看起来像9 5 2 7 6 10 8 4 3 1那么为什么不直接使用array_push$used,$deck[$i]?
// removes dealt cards from the deck
unset($deck[$i]);
// removes dealt cards from the deck then put them in the discard pile
$used[$i] = $deck[$i];
<?php
/*
This code creates a deck of cards and deals them
It also creates a discard pile of used cards. It never
deals duplicates and will re-shuffle the deck if it
gets under 7 cards.
*/
dealCards();
/*
This function creates the deck of cards.
It will deal out 5 cards and remove those
5 cards from the deck.
*/
function dealCards() {
$used = array();
// checks to see if a session has been created
if (isset($_SESSION["deck"])) {
$deck = $_SESSION["deck"];
} else {
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
// will use the same deck until it has 7 or less cards
if (count($deck) > 7) {
if (isset($_SESSION["used"])) {
$used = $_SESSION["used"];
} else {
$_SESSION["used"] = $used;
}
// deals 5 cards from the array $deck
for ($i = 0; $i < 5; $i++) {
print <img src="/main/deck/$deck[$i].png" alt="Card: $deck[$i]" />;
$used[] = $deck[$i];
unset($deck[$i]);
// clears empty values from the array
$deck = array_values(array_filter($deck));
// replaces session data with new deck
$_SESSION["deck"] = $deck;
}
} else {
// shuffle the full 52 cards again if the deck is less than 7 cards
$deck = range(1, 52);
shuffle($deck);
$_SESSION["deck"] = $deck;
}
$_SESSION["used"] = $used;
}
?>