Jump to content

Archived

This topic is now archived and is closed to further replies.

Jessica

array_search(): Wrong datatype for second argument?

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.

Share this post


Link to post
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]

Share this post


Link to post
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*

Share this post


Link to post
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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

×

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.