gpt4 book ai didi

php - 高级数组排序 PHP

转载 作者:行者123 更新时间:2023-11-30 23:14:56 25 4
gpt4 key购买 nike

我正在创建一个纸牌游戏,一个页面创建游戏并将每个玩家的所有纸牌插入 MySQL 数据库。 c1,c2,c3...依此类推由为您选择的随机卡存储。

因此,card1 将作为整数存储在数据库中,如 c1,其余 12 张卡片依此类推。

但是

卡片从数据库返回到游戏页面后,会经过一个数组,用卡片图片进行组织。然后它们通过两个数组显示。

            echo '<form input="index.php" method="post">';
echo $cards[$fetch['c1']].'<input class="check" type="checkbox" name="c1c">';
echo $cards[$fetch['c2']].'<input class="check" type="checkbox" name="c2c">';
echo $cards[$fetch['c3']].'<input class="check" type="checkbox" name="c3c">';
echo $cards[$fetch['c4']].'<input class="check" type="checkbox" name="c4c">';
echo $cards[$fetch['c5']].'<input class="check" type="checkbox" name="c5c">';
echo $cards[$fetch['c6']].'<input class="check" type="checkbox" name="c6c">';
echo $cards[$fetch['c7']].'<input class="check" type="checkbox" name="c7c">';
echo $cards[$fetch['c8']].'<input class="check" type="checkbox" name="c8c">';
echo $cards[$fetch['c9']].'<input class="check" type="checkbox" name="c9c">';
echo $cards[$fetch['c10']].'<input class="check" type="checkbox" name="c10c">';
echo $cards[$fetch['c11']].'<input class="check" type="checkbox" name="c11c">';
echo $cards[$fetch['c12']].'<input class="check" type="checkbox" name="c12c">';
echo $cards[$fetch['c13']].'<input class="check" type="checkbox" name="c13c">';
print_r($fetch);

$cards 是图片分类器。

$fetch 是从数据库返回的数组。

            $query = "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 FROM games WHERE comp = 0";
$stmt = $db->prepare($query);
$stmt->execute();

$fetch = $stmt->fetch(PDO::FETCH_ASSOC);

asort($fetch);

现在这段代码为我提供了我所需要的,print_r 返回

数组 ( [c9] => 3 [c5] => 6 [c10] => 8 [c11] => 13 [c13] => 15 [c3] => 16 [c2] => 18 [c1] => 19 [c4] => 24 [c8] => 40 [c12] => 44 [c7] => 45 [c6] => 47)有组织的升序,但是当我显示卡片时,我不知道如何让它们按那个顺序运行,因为键是数据库中的列名,而我不能在显示中要求的值。

所以我的基本问题是,我如何对卡片进行排序,以便它们遍历我当前的数组以将图片按升序排列到卡片?

如果需要;

            $cards = array(
"0" => "<img src=\"cards/dead.png\"/>",
"1" => "<img src=\"cards/2_of_spades.png\"/>",
"2" => "<img src=\"cards/3_of_spades.png\"/>",
"3" => "<img src=\"cards/4_of_spades.png\"/>",
"4" => "<img src=\"cards/5_of_spades.png\"/>",
"5" => "<img src=\"cards/6_of_spades.png\"/>",
"6" => "<img src=\"cards/7_of_spades.png\"/>",
"7" => "<img src=\"cards/8_of_spades.png\"/>",
"8" => "<img src=\"cards/9_of_spades.png\"/>",
"9" => "<img src=\"cards/10_of_spades.png\"/>",
"10" => "<img src=\"cards/jack_of_spades2.png\"/>",
"11" => "<img src=\"cards/queen_of_spades2.png\"/>",
"12" => "<img src=\"cards/king_of_spades2.png\"/>",
"13" => "<img src=\"cards/ace_of_spades.png\"/>",

"14" => "<img src=\"cards/2_of_clubs.png\"/>",
"15" => "<img src=\"cards/3_of_clubs.png\"/>",
"16" => "<img src=\"cards/4_of_clubs.png\"/>",
"17" => "<img src=\"cards/5_of_clubs.png\"/>",
"18" => "<img src=\"cards/6_of_clubs.png\"/>",
"19" => "<img src=\"cards/7_of_clubs.png\"/>",
"20" => "<img src=\"cards/8_of_clubs.png\"/>",
"21" => "<img src=\"cards/9_of_clubs.png\"/>",
"22" => "<img src=\"cards/10_of_clubs.png\"/>",
"23" => "<img src=\"cards/jack_of_clubs2.png\"/>",
"24" => "<img src=\"cards/queen_of_clubs2.png\"/>",
"25" => "<img src=\"cards/king_of_clubs2.png\"/>",
"26" => "<img src=\"cards/ace_of_clubs.png\"/>",

"27" => "<img src=\"cards/2_of_hearts.png\"/>",
"28" => "<img src=\"cards/3_of_hearts.png\"/>",
"29" => "<img src=\"cards/4_of_hearts.png\"/>",
"30" => "<img src=\"cards/5_of_hearts.png\"/>",
"31" => "<img src=\"cards/6_of_hearts.png\"/>",
"32" => "<img src=\"cards/7_of_hearts.png\"/>",
"33" => "<img src=\"cards/8_of_hearts.png\"/>",
"34" => "<img src=\"cards/9_of_hearts.png\"/>",
"35" => "<img src=\"cards/10_of_hearts.png\"/>",
"36" => "<img src=\"cards/jack_of_hearts2.png\"/>",
"37" => "<img src=\"cards/queen_of_hearts2.png\"/>",
"38" => "<img src=\"cards/king_of_hearts2.png\"/>",
"39" => "<img src=\"cards/ace_of_hearts.png\"/>",

"40" => "<img src=\"cards/2_of_diamonds.png\"/>",
"41" => "<img src=\"cards/3_of_diamonds.png\"/>",
"42" => "<img src=\"cards/4_of_diamonds.png\"/>",
"43" => "<img src=\"cards/5_of_diamonds.png\"/>",
"44" => "<img src=\"cards/6_of_diamonds.png\"/>",
"45" => "<img src=\"cards/7_of_diamonds.png\"/>",
"46" => "<img src=\"cards/8_of_diamonds.png\"/>",
"47" => "<img src=\"cards/9_of_diamonds.png\"/>",
"48" => "<img src=\"cards/10_of_diamonds.png\"/>",
"49" => "<img src=\"cards/jack_of_diamonds2.png\"/>",
"50" => "<img src=\"cards/queen_of_diamonds2.png\"/>",
"51" => "<img src=\"cards/king_of_diamonds2.png\"/>",
"52" => "<img src=\"cards/ace_of_diamonds.png\"/>"
);

最佳答案

使用 foreach ( doco here ) 应该以正确的顺序返回它们。

foreach($fetch as $cardId => $card) {
echo 'CardID: ' . $cardId . '<br>';
echo 'Card: ' . $cards[$card] . '<br>';
}

其中 $cardId 是 key (例如“c1”),$card 是卡的 ID 值(例如 3)。

PHPFiddle example

关于php - 高级数组排序 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18369904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com