高级数组排序PHP

时间:2013-08-22 00:38:41

标签: php mysql

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

所以,card1将作为整数存储在数据库中为c1,等等剩余的12张卡。

BUT

从数据库返回游戏页面上的牌之后,他们会通过一个阵列来组织卡片图片。然后它们通过两个数组显示。例如。

            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返回

Array ( [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\"/>"
            );

1 个答案:

答案 0 :(得分:0)

使用foreachdoco here)应按正确顺序返回。

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

$cardId是密钥(例如'c1'),$card是卡的ID值(例如3)。

PHPFiddle example