Vince889 Posted July 15, 2010 Share Posted July 15, 2010 I should know this, but i'm having a bad day. If I have: $s = array(); $s[0] = "blah."; $s[1] = "blah blah."; $s[2] = "more blah."; echo array_rand($s,1); Why it return a random number and a warning: Warning: array_rand() [function.array-rand]: First argument has to be an array in [filename here] on line 140 I'm just trying to pull a random element from the array without using a complex formula. Now I've done this before but damn, like I said, my day has been full of sh*t. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/ Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 I dunno. I just cut and pasted it, and it works for me. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086205 Share on other sites More sharing options...
Vince889 Posted July 15, 2010 Author Share Posted July 15, 2010 I dunno. I just cut and pasted it, and it works for me. Its just giving numbers [the array key number] and not the actual content. I can't seem to remember how to do this. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086207 Share on other sites More sharing options...
Maq Posted July 15, 2010 Share Posted July 15, 2010 Try this: $s = array("blah.", "blah blah.", "more blah."); $rand = array_rand($s); echo $s[$rand]; ?> Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086208 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 This works for me, but I didn't look to see if there's a reason you shouldn't use it, so you might want to peek at the manual. echo array_rand(array_flip($s),1); Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086210 Share on other sites More sharing options...
Vince889 Posted July 15, 2010 Author Share Posted July 15, 2010 Try this: <?php $s = array("blah.", "blah blah.", "more blah."); $rand = array_rand($s); echo $s[$rand]; ?> Excellent, man. It works. Thank you very much.. solved :] And Pickachu, you're right as well. Don't mind me i'm in my own little worldddd. Sorry for wasting you folk's time. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086211 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 Wasting our time? Surely you jest . . . There's more than one way to do most things, glad it's figured out. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086212 Share on other sites More sharing options...
Maq Posted July 15, 2010 Share Posted July 15, 2010 There's more than one way to do most things, glad it's figured out. Yep, there's probably 10 different ways to do this just depends on how you want to accomplish it. Link to comment https://forums.phpfreaks.com/topic/207786-quick-array-question/#findComment-1086214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.