Jump to content


Photo

array_search(): Wrong datatype for second argument?


  • Please log in to reply
4 replies to this topic

#1 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 06 June 2006 - 09:15 PM

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

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];
            }
        }
    }    
}

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.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 06 June 2006 - 09:22 PM

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]
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 06 June 2006 - 09:25 PM

[!--quoteo(post=380782:date=Jun 6 2006, 04:22 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 6 2006, 04:22 PM) View Post[/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*
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 06 June 2006 - 09:26 PM

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

NoGray.com


#5 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 06 June 2006 - 09:28 PM

[!--quoteo(post=380786:date=Jun 6 2006, 04:26 PM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 6 2006, 04:26 PM) View Post[/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

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

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

Thanks!
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users