简体   繁体   中英

Create an array from an array and keep the same order

I've created a very simple deck of cards. I'm using 3 arrays, $deck, $cards, & $used. $deck is the cards in play, $cards is a comparison array, and $used is a discard array. When cards are used from $deck they get place in the $used array. My issue is when they are placed in the $used array they are put into numeric order. I don't want this. I need them to be in the same order that they are removed from the $deck array. The full code is below.

<?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>";

}

?>

First thing that comes in my mind is that when you unset your deck array

    // removes dealt cards from the deck
    unset($deck[$i]);

you can put the card in the $used array

    // removes dealt cards from the deck then put them in the discard pile
    $used[$i] = $deck[$i];

But this would work only for the first time. In a second usage of dealCards it would be overwritten. Plus, the $used array will surely have some empty position in it. Do you need them to be stored permanently?

I've got it working. The code needs cleaned up a bit but at least it works. Thanks for the help Wrikken and everyone else. Below is the modified working code.

<?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;

}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM