cooldude832 Posted August 1, 2007 Share Posted August 1, 2007 I have this code that is saying my array_rand is not an array being provided error: Warning: array_rand() [function.array-rand]: First argument has to be an array in functions.php on line 118 Warning: array_rand() [function.array-rand]: First argument has to be an array in functions.php on line 118 <?php require_once("battle.php"); $i = 0; $j = $i; $tempfields = ""; $tempvalues = ""; $num_ops = $num_ops-1; if($num_ops<1){$num_ops = 1;} while($i <= $num_ops){ $j++; $npc_key = array_rand($npc_name); $npc_insert_name[$i] = $npc_name[$npc_key]; $npc_insert_HP[$i] = $npc_HP[$npc_key]; $npc_insert_attack[$i] = $npc_attack[$npc_key]; $npc_insert_defense[$i] = $npc_defense[$npc_key]; $npc_insert_weapon[$i] = $npc_weapon[$npc_key]; //Adds a comma on afterwards if($tempfields != ""){$tempfields .= ",";} $tempfields .= "Name".$j.", HP".$j.", Attack".$j.", Defense".$j; //Adds a comma on afterwards if($tempvalues != ""){$tempvalues .= ",";} $tempvalues .= "'".$npc_insert_name[$i]."',"."'".$npc_insert_HP[$i]."',"."'".$npc_insert_attack[$i]."',"."'".$npc_insert_defense[$i]."',"."'".$npc_insert_weapon[$i]; $i++; } ?> This is battle.php <?php $x = $row['X']; $y = $row['Y']; $num_ops = array(); $npc_name = array(); $npc_HP = array(); $npc_attack = array(); $npc_defense = array(); $npc_weapon = array(); //Reigion One (from (5,5) to (40,40) if($x>5 && $x <40 && $y<40 && $y>5){ $num_ops = rand(1,1); $npc_name[] = "Hod Goblin"; $npc_HP[] = rand(25,40); $npc_attack[] = rand(5,10); $npc_defense[] = rand(5,10); $npc_weapon[] = "Slap Attack"; } ?> Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/ Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 check the manual for the function. it triggers an error because the count of the array is less than the optional $num_req parameter, which defaults to 1. this makes sense - how are you supposed to pick a random entry from an empty array? Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313358 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 that is what i'm thinking, but i'm pretty sure its entering that if logic in battle, that must be the error than in the logic i'll try it with out the if and see what happens. Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313361 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 tried it without the if logic on the battle and no good, i think i'll have to generate the random key differently Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313363 Share on other sites More sharing options...
PC Nerd Posted August 1, 2007 Share Posted August 1, 2007 $num = rand(0,20); $ARRAY[$num]; OR shuffle($ARRAY); $ARRAY[0]; thos should work. in the first one, maybe to an array length count... and then replace the last argument with the length or something. gdkl Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313366 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 one sec i'm trying something else, I didn't want to use shuffle in this case, but i had thought of it Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313368 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 still no good tried doing this functions.php <?php function attackers() { require_once("battle.php"); connectSQL(); $tempq = "INSERT INTO `Battle` (UserID,".$tempfields.") VALUES('".$_SESSION['UserID']."',".$tempvalues.")"; mysql_query($tempq) or die("tempvalues = ".$tempvalues."<br/>tempfields = ".$tempfields."<br/>Done"); echo "Battle Has been intialzed"; }//end of attackres ?> battle.php <?php $num_ops = 1; $i = 1; while ($i<=$num_ops){ $npc_name[] = "Hod Goblin"; $npc_HP[] = rand(25,40); $npc_attack[] = rand(5,10); $npc_defense[] = rand(5,10); $npc_weapon[] = "Slap Attack"; $tempfields = ""; $tempvalues = ""; //Adds a comma on afterwards if($tempfields != ""){$tempfields .= ",";} $tempfields .= "Name".$i.", HP".$i.", Attack".$i.", Defense".$i; //Adds a comma on afterwards if($tempvalues != ""){$tempvalues .= ",";} $tempvalues .= "'".$npc_name[$i]."',"."'".$npc_HP[$i]."',"."'".$npc_attack[$i]."',"."'".$npc_defense[$i]."',"."'".$npc_weapon[$i]; $i++; } ?> Now i'm really baffeled because the while loop isnt even starting Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313370 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 how do you know the while() loop isn't starting? what evidence is there? it looks like it should run just once. trying adding a dummy echo line on the first line of the while() loop to see if it's running. Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313373 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 my query says or die and print out the tempfields temp query which are blank i made it to $i =0 and while($i< $num_ops) instead which ain't a big deal but still it doesn't make snes Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313376 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 as i said, add a dummy echo line to the start of the while() loop to ensure that it's the while() loop not executing. the next step would be to echo all the variables declared in the battle.php file from within the function, to ensure that it's executing correctly in scope. Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313380 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 scope is sounding like the issue here, I can get it to echo out tempvalues/fields fine in the top level document, but when i try and get it to echo then out in the function inside functions.php it can't and thsu my query is an empty query. The values are generated from battle.php and then used in functions.php inside a function, any ideas how to fix this? Even though the query inserts nothing the query still executes and a new row is added functions.php <?php function attackers() { require_once("battle.php"); connectSQL(); $tempq = "INSERT INTO `Battle` (".$tempfields.") VALUES(".$tempvalues.")"; mysql_query($tempq) or die(mysql_error()); echo "Battle Has been intialzed<br/>Temp Fields:" .$tempfields."<br/>Temp Values: ".$tempvalues; }//end of attackres ?> Main document (this part echos out tempfields/tempvalues perfectly fine <?php if($view == "battle"){ echo "<tr><td>Battle Time<br/>Tempfields: ".$tempfields."<br/>Temp Values: ".$tempvalues."</td></tr></table>"; } ?> battle.php (source of variables) <?php $num_ops = 1; $i = 0; $j = 0; $tempfields = "UserID"; $tempvalues = "'".$_SESSION['UserID']."'"; while ($i<$num_ops){ $j++; $npc_name[] = "Hod Goblin"; $npc_HP[] = rand(25,40); $npc_attack[] = rand(5,10); $npc_defense[] = rand(5,10); $npc_weapon[] = "Slap Attack"; //Adds a comma on afterwards $tempfields .= ", Name".$j.", HP".$j.", Attack".$j.", Defense".$j; //Adds a comma on afterwards $tempvalues .= ",'".$npc_name[$i]."',"."'".$npc_HP[$i]."',"."'".$npc_attack[$i]."',"."'".$npc_defense[$i]."',"."'".$npc_weapon[$i]."'"; $i++; $j++; } ?> Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313382 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 if you're trying to pass them from the main scope to the function, you'll either need to declare them global within the function or pass them manually. if the variables are already defined in the main scope, no need to include battle.php from within attackers(): <?php function attackers($tempfields, $tempvalues) { connectSQL(); $tempq = "INSERT INTO `Battle` (".$tempfields.") VALUES(".$tempvalues.")"; mysql_query($tempq) or die(mysql_error()); echo "Battle Has been intialzed<br/>Temp Fields:" .$tempfields."<br/>Temp Values: ".$tempvalues; }//end of attackres ?> Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313387 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 okay now i just gotta figure out the last issues Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313391 Share on other sites More sharing options...
cooldude832 Posted August 1, 2007 Author Share Posted August 1, 2007 Finally got the kincks worked out, didn't know you have to redeclare globals in a function like that, never had that issue before, oh well you learn something everyday Link to comment https://forums.phpfreaks.com/topic/62934-solved-array_rand-saying-its-not-an-array-when-it-is/#findComment-313393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.