foreach和while循环使用数组来复制数据

时间:2016-07-20 02:12:09

标签: php mysql arrays

这是我写的一个函数:

function createDeck($deckId) {
    global $dbConnection;
    $queryCreateDeck = "SELECT c.card_name, dc.qty
                                    FROM decks AS d
                                      JOIN decks_to_cards AS dc
                                        ON d.deck_id = dc.deck_id
                                      JOIN cards AS c
                                        ON dc.card_id = c.card_id
                                    WHERE d.deck_id = {$deckId}
                                    ORDER BY c.card_name";
    $createDeck = mysqli_query($dbConnection, $queryCreateDeck);

    $deckArray = array();
    $deckFinal = array();
    while ($deck = mysqli_fetch_assoc($createDeck)) {
        $deckArray[] = $deck;
    };
    foreach ($deckArray["card_name"] as $card) {
        while($deckArray["qty"] > 0) {
            array_push($deckFinal, $deckArray["card_name"]);
        };
    };
    print_r($deckArray);

};

这是数组$deckArray输出的内容:

Array ( 
    [0] => Array ( 
        [card_name] => Archangel Avacyn 
        [qty] => 4 
    ) 
    [1] => Array ( 
        [card_name] => Canopy Vista 
        [qty] => 4 
    ) 
    [2] => Array ( 
        [card_name] => Dromoka's Command 
        [qty] => 4 
    ) 
    [3] => Array ( 
        [card_name] => Evolutionary Leap 
        [qty] => 2 
    )
    4] => Array ( 
        [card_name] => Forest 
        [qty] => 9 
    ) 
    [5] => Array ( 
        [card_name] => Fortified Village 
        [qty] => 4 
    ) 
    [6] => Array ( 
        [card_name] => Gideon, Ally of Zendikar 
        [qty] => 4 
    ) 
    [7] => Array ( 
        [card_name] => Hangarback Walker 
        [qty] => 4 
    ) 
    [8] => Array ( 
        [card_name] => Nissa, Voice of Zendikar 
        [qty] => 4 
    ) 
    [9] => Array ( 
        [card_name] => Oath of Nissa 
        [qty] => 4 
    ) 
    [10] => Array ( 
        [card_name] => Plains 
        [qty] => 7 
    )
    [11] => Array ( 
        [card_name] => Secure the Wastes 
        [qty] => 2 
    ) 
    [12] => Array ( 
        [card_name] => Sylvan Advocate 
        [qty] => 4 
    ) 
    [13] => Array ( 
        [card_name] => Tragic Arrogance 
        [qty] => 2 
    ) 
    [14] => Array ( 
        [card_name] => Westvale Abbey 
        [qty] => 2
    ) 
)

我正在尝试为该数组中的qty创建一个包含每个card_name实例的数组。

它在foreach和随后的循环中抛出错误告诉我索引不正确。

然而,当我看到数组时,似乎那些肯定是索引。任何帮助都会非常感激,我一直在抨击我的大脑。

2 个答案:

答案 0 :(得分:2)

如果您只是尝试过滤条件WHERE qty > 0,为什么不在查询中执行此操作,然后获取卡名称(通常的提取)。这条路的路线要短得多。

如果您转到PHP路由,只需在while块中执行:

$deckFinal = array();
while ($deck = mysqli_fetch_assoc($createDeck)) {
    if($deck['qty'] > 0) { // greater than zero
        $deckFinal[] = $deck['card_name']; // push card name
    }
}

无需重新分配到另一个容器,然后使另一个过滤器传递到另一个数组。

我通过使用预处理语句更进一步,不要在查询语句中直接注入变量。

改为使用占位符:

function createDeck($dbConnection, $deckId) {
    $queryCreateDeck = ".
        SELECT c.card_name, dc.qty
            FROM decks AS d
            JOIN decks_to_cards AS dc
                ON d.deck_id = dc.deck_id
              JOIN cards AS c
                ON dc.card_id = c.card_id
            WHERE d.deck_id = ?
            AND dc.qty > 0
            ORDER BY c.card_name
    ";
    $createDeck = $queryCreateDeck->prepare($queryCreateDeck);
    $createDeck->bind_param('i', $deckId);
    $createDeck->execute();
    $createDeck->bind_result($card_name, $qty);

    $deckFinal = array();
    while ($createDeck->fetch())) {
        $deckFinal[] = $card_name;
        // $deckFinal[] = ['card_name' => $card_name, 'qty' => $qty];
    }

    return $deckFinal;
}

答案 1 :(得分:0)

这给了我理想的结果,我只是在思考整个过程。感谢你们!

while ($deck = mysqli_fetch_assoc($createDeck)) {
             while($deck['qty'] > 0) {
                $deck['qty']--; 
                $deckFinal[] = $deck['card_name']; // push card name
            };
        };