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? 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]; 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]}"; 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. Link to comment https://forums.phpfreaks.com/topic/296990-linking-arrays/#findComment-1514755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.