silverglade Posted July 31, 2010 Share Posted July 31, 2010 Hi, I am trying to get a random monster from my multidimensional "monsters" array. I have array monsters, and inside that I have array "ocean" and inside that are "fish", "shark" etc. like the below, and I am trying to pick out a random monster from the Ocean array. Any help greatly appreciated. thank you. Derek here is the array $monsters = array ( 'Ocean'=>array ( 'octalisk'=>array ( 'name'=>'octalisk', 'hp'=>100, 'damageLow'=>1, 'damageHigh'=>15, 'exp'=>10, 'ac'=>'HAVE TO LOOK THIS UP', 'monsterInitiativeModifier'=>'HAVE TO LOOK THIS UP'), 'shark', 'eel' ), 'Desert'=>array ( 'sand_snake' ), 'Forest'=>array ( 'frog', 'lizard', 'spider' ) ); Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/ Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 is this right please? $currentMonster = $monsters[array_rand($monsters['Ocean'])]; Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093508 Share on other sites More sharing options...
Jessica Posted July 31, 2010 Share Posted July 31, 2010 is this right please? $currentMonster = $monsters[array_rand($monsters['Ocean'])]; Did it work? Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093513 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 no, it didn't echo out anything. when I echoed out the variable. it just outputs "the current monster is ". and doesn't show the value . echo "the current monster is ".$currentMonster; Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093514 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 ok its bed time. Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093521 Share on other sites More sharing options...
jcbones Posted July 31, 2010 Share Posted July 31, 2010 Multi- dimensional arrays are a little more difficult to work with in this way. Try something like this: <?php $monsters = array ( 'Ocean'=>array ( 'octalisk'=>array ( 'name'=>'octalisk', 'hp'=>100, 'damageLow'=>1, 'damageHigh'=>15, 'exp'=>10, 'ac'=>'HAVE TO LOOK THIS UP', 'monsterInitiativeModifier'=>'HAVE TO LOOK THIS UP'), 'shark', 'eel' ), 'Desert'=>array ( 'sand_snake' ), 'Forest'=>array ( 'frog', 'lizard', 'spider' ) ); $random = array_rand($monsters); $monster = array_rand($monsters[$random]); if(is_array($monsters[$random][$monster])) { $currentMonster = $monsters[$random][$monster]['name']; } else { $currentMonster = $monsters[$random][$monster]; } echo $currentMonster; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093525 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 wow thank you so much for your time and that code! I have to admit it's pretty mind boggling even that small bit. LOL! But I'm going to keep looking at it so I understand it. I'm definitely going to try it out so thanks very much!! :D Derek Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093569 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 HAHAH!! it WORKS!! THANK YOU VERY MUCH!! wow. you're a guru in my book, I waited pretty long for an answer so THANK YOU!!! awesome. thanks. Derek :D :D Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093577 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 The code works great, only one problem, it echoes out random monsters from the monsters array, and I wanted it to echo out random monsters from the "Ocean" array that is inside of it, so I can generate random encounters when a person enters a certain habitat. Any more help GREATLY appreciated. Thanks for the code. Derek Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093601 Share on other sites More sharing options...
jcbones Posted July 31, 2010 Share Posted July 31, 2010 This line gets the random area's $random = array_rand($monsters); Simply change it to: $random = 'Ocean'; Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093608 Share on other sites More sharing options...
silverglade Posted July 31, 2010 Author Share Posted July 31, 2010 haha jcbones you are a coding GENIUS!! this is great, now I can continue making my game. Thank you so much for your time and help. Derek :D :D Quote Link to comment https://forums.phpfreaks.com/topic/209435-need-an-example-using-php_rand-on-a-multidimensional-array-please/#findComment-1093611 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.