Jump to content

Recommended Posts

Hi all

 

I've just started building a small poker application but hit a bit of a problem.

I've added my code below so you can see what is going on.

 

I've created two seperate arrays for number and suit of card. Loop through them both concatonating both values to give me individual cards which works fine.

 

BUT........................ every item in the array has an index of 0. So all of the card values are in the same index. I want each card value to be seperate.

 

I was thinking that I would need to comma seperate each value then explode via the comma but I thought this was a little long winded as I thought each value would be seperate at the end of the foreach loop.

 

Can anyone please point me in the right direction.

 

Thanks

Adi

 

 

PHP CLASS

card_deck.php

class card_deck {

 

public $results;

public $cards;

public function setDeck() {

global $results;

$number = array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');

$suit = array('H', 'D', 'C', 'S');

 

foreach($number as $n)

{

foreach($suit as $s)

{

$this->results = array($n . $s);

var_dump($this->results);

}

}

 

}

}

 

 

index.php

 

$deck = new card_deck();

 

$deck->setDeck();

<?php
class card_deck {

   public $results;
   public $cards;

   public function setDeck() {
global $results;

$number = array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');
$suit   = array('H', 'D', 'C', 'S');

foreach($number as $n) {
foreach($suit as $s) {
$this->results[] = $n . $s;
}
}
var_dump($this->results);
   }
}



$deck = new card_deck();
$deck->setDeck(); 

/*Result:

[b]array[/b]
 0 => string '2H' [i](length=2)[/i]
 1 => string '2D' [i](length=2)[/i]
 2 => string '2C' [i](length=2)[/i]
 3 => string '2S' [i](length=2)[/i]
 4 => string '3H' [i](length=2)[/i]
 5 => string '3D' [i](length=2)[/i]
 6 => string '3C' [i](length=2)[/i]
 7 => string '3S' [i](length=2)[/i]
 8 => string '4H' [i](length=2)[/i]
 9 => string '4D' [i](length=2)[/i]
 10 => string '4C' [i](length=2)[/i]
 11 => string '4S' [i](length=2)[/i]
 12 => string '5H' [i](length=2)[/i]
 13 => string '5D' [i](length=2)[/i]
 14 => string '5C' [i](length=2)[/i]
 15 => string '5S' [i](length=2)[/i]
 16 => string '6H' [i](length=2)[/i]
 17 => string '6D' [i](length=2)[/i]
 18 => string '6C' [i](length=2)[/i]
 19 => string '6S' [i](length=2)[/i]
 20 => string '7H' [i](length=2)[/i]
 21 => string '7D' [i](length=2)[/i]
 22 => string '7C' [i](length=2)[/i]
 23 => string '7S' [i](length=2)[/i]
 24 => string '8H' [i](length=2)[/i]
 25 => string '8D' [i](length=2)[/i]
 26 => string '8C' [i](length=2)[/i]
 27 => string '8S' [i](length=2)[/i]
 28 => string '9H' [i](length=2)[/i]
 29 => string '9D' [i](length=2)[/i]
 30 => string '9C' [i](length=2)[/i]
 31 => string '9S' [i](length=2)[/i]
 32 => string '10H' [i](length=3)[/i]
 33 => string '10D' [i](length=3)[/i]
 34 => string '10C' [i](length=3)[/i]
 35 => string '10S' [i](length=3)[/i]
 36 => string 'JH' [i](length=2)[/i]
 37 => string 'JD' [i](length=2)[/i]
 38 => string 'JC' [i](length=2)[/i]
 39 => string 'JS' [i](length=2)[/i]
 40 => string 'QH' [i](length=2)[/i]
 41 => string 'QD' [i](length=2)[/i]
 42 => string 'QC' [i](length=2)[/i]
 43 => string 'QS' [i](length=2)[/i]
 44 => string 'KH' [i](length=2)[/i]
 45 => string 'KD' [i](length=2)[/i]
 46 => string 'KC' [i](length=2)[/i]
 47 => string 'KS' [i](length=2)[/i]
 48 => string 'AH' [i](length=2)[/i]
 49 => string 'AD' [i](length=2)[/i]
 50 => string 'AC' [i](length=2)[/i]
 51 => string 'AS' [i](length=2)[/i]
*/

// Result

 

array

0 => string '2H' (length=2)

1 => string '2D' (length=2)

2 => string '2C' (length=2)

3 => string '2S' (length=2)

4 => string '3H' (length=2)

5 => string '3D' (length=2)

6 => string '3C' (length=2)

7 => string '3S' (length=2)

8 => string '4H' (length=2)

9 => string '4D' (length=2)

10 => string '4C' (length=2)

11 => string '4S' (length=2)

12 => string '5H' (length=2)

13 => string '5D' (length=2)

14 => string '5C' (length=2)

15 => string '5S' (length=2)

16 => string '6H' (length=2)

17 => string '6D' (length=2)

18 => string '6C' (length=2)

19 => string '6S' (length=2)

20 => string '7H' (length=2)

21 => string '7D' (length=2)

22 => string '7C' (length=2)

23 => string '7S' (length=2)

24 => string '8H' (length=2)

25 => string '8D' (length=2)

26 => string '8C' (length=2)

27 => string '8S' (length=2)

28 => string '9H' (length=2)

29 => string '9D' (length=2)

30 => string '9C' (length=2)

31 => string '9S' (length=2)

32 => string '10H' (length=3)

33 => string '10D' (length=3)

34 => string '10C' (length=3)

35 => string '10S' (length=3)

36 => string 'JH' (length=2)

37 => string 'JD' (length=2)

38 => string 'JC' (length=2)

39 => string 'JS' (length=2)

40 => string 'QH' (length=2)

41 => string 'QD' (length=2)

42 => string 'QC' (length=2)

43 => string 'QS' (length=2)

44 => string 'KH' (length=2)

45 => string 'KD' (length=2)

46 => string 'KC' (length=2)

47 => string 'KS' (length=2)

48 => string 'AH' (length=2)

49 => string 'AD' (length=2)

50 => string 'AC' (length=2)

51 => string 'AS' (length=2)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.