0

I'm a total newb to PHP and I'm having a logic problem in my code (array_rand($arrCards,1) is running out of elements before it should). So I've done a var_dump of the array and I get this output (brief excerpt):

    array (size=52)
  0 => 
    object(Card)[2]
      private 'suit' => string 'heart' (length=5)
      private 'rank' => string 'a' (length=1)
  1 => 
    object(Card)[3]
      private 'suit' => string 'heart' (length=5)
      private 'rank' => string '2' (length=1)
  2 => 
    object(Card)[4]
      private 'suit' => string 'heart' (length=5)
      private 'rank' => string '3' (length=1)

What I don't understand is the index in 'object(Card)[index]'. It seems to hint as to why the array_rand() is running out of elements.

Here's my Card class:

class Card {
    //properties
    private $suit;
    private $rank;

    //constructor
    public function __construct($r, $s) {
        $this->rank = $r;
        $this->suit = $s;
    }

    //methods
    public function getSuit() {
        return $suit;
    }

    public function getRank() {
        return $rank;
    }

}

And here is how I create each card and push it into each array:

//constructor
    public function __construct() {
        $this->arrCards = array();

        $objCard = new Card("a", "heart");
        array_push($this->arrCards, $objCard);
        $objCard = new Card("2", "heart");
        array_push($this->arrCards, $objCard);
        $objCard = new Card("3", "heart");
blah blah continued...
erv
  • 593
  • 8
  • 27
  • 2
    see this https://stackoverflow.com/questions/8787893/what-is-some-number-next-to-objectsomeclass-in-var-dump-of-an-object-i-hav I hope it helps – Alex Andrei Aug 22 '17 at 05:41
  • it seems to be a different notation to what I'm getting. But assuming it translates, then this number inside my [ ] has nothing to do with indices and therefore probably isn't causing the problem with array_rand()? – erv Aug 22 '17 at 05:52
  • Your code doesn't show how your using the `array_rand()`, it's difficult to tell without that part of the code. – Nigel Ren Aug 22 '17 at 06:06
  • i would say the different notation is because you have your objects in an array, didn't test though. as to your question, no, it doesn't have anything to do with any rand issues you may have – Alex Andrei Aug 22 '17 at 06:11
  • Cheers. I'm going to post a new question specifically with the array_rand problem I'm having. – erv Aug 22 '17 at 06:14

1 Answers1

0

I would suggest you loop your array push instead of manually do everything. This is how I would do it and var_dump give length of 52. Also note that you should use $this on getSuit() and getRank().

$cards_array = array();
$ranks = array('a',2,3,4,5,6,7,8,9,10,'j','q','k');
$suits = array('heart', 'spade', 'diamond', 'club');

foreach ($suits as $suit) {
    foreach($ranks as $rank) {
        $cards_array[] = new Card($rank, $suit);
    }
}

var_dump($cards_array);

class Card {
    //properties
    private $suit;
    private $rank;

    //constructor
    public function __construct($r, $s) {
        $this->rank = $r;
        $this->suit = $s;
    }

    //methods
    public function getSuit() {
        return $this->suit;
    }

    public function getRank() {
        return $this->rank;
    }
}
J-P Vuorela
  • 11
  • 1
  • 4
  • Cheers for that. I noticed the $rank and $suit "$this->" issue as I posted the code (my program hasn't actually got that far without crashing yet.. ;) ). – erv Aug 22 '17 at 06:04