Jessica Posted June 6, 2006 Share Posted June 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11358-array_search-wrong-datatype-for-second-argument/ Share on other sites More sharing options...
Buyocat Posted June 6, 2006 Share Posted June 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11358-array_search-wrong-datatype-for-second-argument/#findComment-42591 Share on other sites More sharing options...
Jessica Posted June 6, 2006 Author Share Posted June 6, 2006 [!--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.phpThanks anyway :)*SOLVED* Quote Link to comment https://forums.phpfreaks.com/topic/11358-array_search-wrong-datatype-for-second-argument/#findComment-42593 Share on other sites More sharing options...
nogray Posted June 6, 2006 Share Posted June 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11358-array_search-wrong-datatype-for-second-argument/#findComment-42595 Share on other sites More sharing options...
Jessica Posted June 6, 2006 Author Share Posted June 6, 2006 [!--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! Quote Link to comment https://forums.phpfreaks.com/topic/11358-array_search-wrong-datatype-for-second-argument/#findComment-42597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.