cloudll Posted June 23, 2015 Share Posted June 23, 2015 I was wondering if there is a way to link two arrays together. I currently have $enemyNames = array("The Pain", "The Fear", "The End"); $enemyImage = array("img01", "img02", "img03"); I am then using array_rand to generate a random "enemy" $enemyName = $enemyNames[array_rand($enemyNames)]; I would ideally like to have the first enemy name linked with img01, and the second linked to img02 etc. I am not sure how I would link them together while still using array_rand. Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/296990-linking-arrays/ Share on other sites More sharing options...
cyberRobot Posted June 23, 2015 Share Posted June 23, 2015 You could utilize the key returned from array_rand(). For example $randomKey = array_rand($enemyNames); $enemyName = $enemyNames[$randomKey]; $currImage = $enemyImage[$randomKey]; Quote Link to comment https://forums.phpfreaks.com/topic/296990-linking-arrays/#findComment-1514749 Share on other sites More sharing options...
Barand Posted June 23, 2015 Share Posted June 23, 2015 or $enemyNames = array("The Pain", "The Fear", "The End"); $enemyImage = array("img01", "img02", "img03"); $combined = array_combine($enemyNames, $enemyImage); $key = array_rand($combined); echo "$key - {$combined[$key]}"; Quote Link to comment https://forums.phpfreaks.com/topic/296990-linking-arrays/#findComment-1514751 Share on other sites More sharing options...
cloudll Posted June 23, 2015 Author Share Posted June 23, 2015 Awesome, thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/296990-linking-arrays/#findComment-1514755 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.