Jump to content

array_search(): Wrong datatype for second argument?


Jessica

Recommended Posts

I have a function for part of my blackjack game I am writing. There is an array $cards[] in the file cards.php.

Here is the part of the function that is causing trouble in game_functions.php

[code]function bj_points($bj_info){
    include_once('cards.php');
    $points     = array();
    $dealer     = $bj_info['dealer'];
    $userhand = $bj_info['userhand'];

    $dpoints     = 0;
    $dpoints2 = 0;
    $ppoints     = 0;
    $ppoints2 = 0;

    $temp = strlen($dealer);
    for($i=0; $i<=$temp; $i=$i+2){
        $card         = substr($dealer, 0, 2);;
        $dealer_hand[] = $card;
        $dealer         = substr($dealer, 2, strlen($dealer));
        $cardkey         = array_search($card, $cards); //<-- line 413
        if($values[$cardkey] == 1){
            $dpoints     += 1;
            $dpoints2 = $dpoints;
            $dpoints2 += 11;
        }else{
            if($bj_info['stage'] < 3){
                $dpoints = $values[$cardkey];
            }else{
                $dpoints += $values[$cardkey];
            }
        }
    }    
}[/code]

And this is the error I get
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: array_search(): Wrong datatype for second argument in
(snip)html/games/game_functions.php on line 413[/quote]

Help?

I tried adding global $cards; to the function but it didn't help.
Link to comment
Share on other sites

What is the $dealer variable? You define it here: $dealer = $bj_info['dealer']; but that doesn't really help. If it isn't a string that would explain the error. Also you don't seem to define $cards here so it may not be set and if it is it may not be an array. Again if $card isn't a string and $cards isn't an array that is the source of the problem.

[a href=\"http://us2.php.net/array_search\" target=\"_blank\"]http://us2.php.net/array_search[/a]
Link to comment
Share on other sites

[!--quoteo(post=380782:date=Jun 6 2006, 04:22 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 6 2006, 04:22 PM) [snapback]380782[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What is the $dealer variable? You define it here: $dealer = $bj_info['dealer']; but that doesn't really help. If it isn't a string that would explain the error. Also you don't seem to define $cards here so it may not be set and if it is it may not be an array. Again if $card isn't a string and $cards isn't an array that is the source of the problem.

[a href=\"http://us2.php.net/array_search\" target=\"_blank\"]http://us2.php.net/array_search[/a]
[/quote]

Every time I post on this forum, I manage to debug it right away :)

I solved the problem by including cards.php at the start of the game_functions file, rather than IN the function.

dealer is a string like 'AH9S' (Ace hearts, 9 spades) which go into the array to print the right images later on. So $card is a string and $cards is an array in cards.php

Thanks anyway :)

*SOLVED*
Link to comment
Share on other sites

make sure the $cards is declared as an array somewhere in your script. I am not sure if you have it in the 'cards.php' file or not. But if you don't have it there, and it's not a global variable that is set as an array, you'll need to add

$cards = array();

before you use it in the array_search() function
Link to comment
Share on other sites

[!--quoteo(post=380786:date=Jun 6 2006, 04:26 PM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 6 2006, 04:26 PM) [snapback]380786[/snapback][/div][div class=\'quotemain\'][!--quotec--]
make sure the $cards is declared as an array somewhere in your script. I am not sure if you have it in the 'cards.php' file or not. But if you don't have it there, and it's not a global variable that is set as an array, you'll need to add

$cards = array();

before you use it in the array_search() function
[/quote]

Yeah it is

[code]$cards         = array();
$cards[1]     = '2H';
$cards[2]     = '3H';
$cards[3]     = '4H';
(snip)[/code]

is cards.php. I had to put it in the file outside of the function and do global $cards;

Thanks!
Link to comment
Share on other sites

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.