Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Basic Array and If statement problem.

Hey there! I am trying to do the following:

1) generate a random number. 2) Compare that number with the number under the ‘card value’ key in the array. 3) If the number is in the array add it to the total variable and remove that whole array from the multi-dimensional array.

I am very new to programming and have the following:

$cards[] = array('card_value' => 1,
               'card_suit' => 'Heart',
               'card_face' => 'Ace');

$cards[] = array('card_value' => 2,
               'card_suit' => 'Heart',
               'card_face' => '');

$cards[] = array('card_value' => 3,
               'card_suit' => 'Heart',
               'card_face' => '');

$cards[] = array('card_value' => 4,
               'card_suit' => 'Heart',
               'card_face' => 'Ace');

$cards[] = array('card_value' => 5,
               'card_suit' => 'Heart',
               'card_face' => '');

$cards[] = array('card_value' => 6,
               'card_suit' => 'Heart',
               'card_face' => '');

$cards[] = array('card_value' => 11,
               'card_suit' => 'Heart',
               'card_face' => 'Jack');

$cards[] = array('card_value' => 12,
               'card_suit' => 'Heart',
               'card_face' => 'Queen');

$cards[] = array('card_value' => 13,
               'card_suit' => 'Heart',
               'card_face' => 'King');

$total_number = 0;

do {
    $randon_number = rand(1,13);
  if ($random_number == $cards('card_value')){
  echo $cards('cards value'); 
  $total_number = $total_number + $randon_number;
  }
  else {
  echo 'not matching value';
  }
} 
while 
($total_number <= 21);

i keep getting the error Function name must be a string. Can anyone help? Thankyou

2 Answers

Joel Bardsley
Joel Bardsley
31,247 Points

You have some inconsistencies with the $random_number variable, twice it has been misspelled ('randon') whereas the if statement has it spelled correctly.

Also $cards('card_value') should use square brackets ie. $cards['card_value'] as you're referencing an array key. Using parentheses means it might think you're passing the string 'card_value' into a function assigned to the $cards variable.

You could use PHP's array_column method to create a temporary single-dimensional array of just card_values, which you could then use the array_search method to search for a matching card value more easily.

I've done a quick workspace with a few notes to implement this, feel free to fork and expand on this for your own needs.

Thank you Joel! I am going to look at this in great detail tomorrow as my head is swimming with code right now and I need a break. Will update you tomorrow when I give it a try for myself.

Thankyou for the help!