ksimpkins Posted February 23, 2010 Share Posted February 23, 2010 I am new to PHP and I was trying to create an array and have it shuffle some write content. The website address is http://www.travelvi.com/template/us-vir ... dings.html -- I am trying shuffle ads randomly please help me with this.. <?php $ads = array (WriteContent('ad-stt-weddings-elisha-orin-photography'), WriteContent('ad-stt-weddings-brandi-mays-videographer')); shuffle($ads); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/ Share on other sites More sharing options...
eli312 Posted February 23, 2010 Share Posted February 23, 2010 <?php $ads = array (WriteContent('ad-stt-weddings-elisha-orin-photography'), WriteContent('ad-stt-weddings-brandi-mays-videographer')); shuffle($ads); ?> Take alook at this http://php.net/manual/en/function.shuffle.php. So your code should be something like this: <?php $numbers = range(1, 20); shuffle($numbers); foreach ($numbers as $number) { echo "$number "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1016988 Share on other sites More sharing options...
teamatomic Posted February 23, 2010 Share Posted February 23, 2010 Your code works as you think it should. You create an array then shuffle it. What is your exact problem or error HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1016995 Share on other sites More sharing options...
lovelyvik293 Posted February 23, 2010 Share Posted February 23, 2010 use foreach($ads as $ads) echo $ads; To echo the code after shuffle function. Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1017001 Share on other sites More sharing options...
ksimpkins Posted March 8, 2010 Author Share Posted March 8, 2010 Sorry everyone.. This is what I am trying to do.. but I would like to only write a certain number from the array.. for example.. if I had 20 items in the array, but I would want it to randomly pick 5 of the them and display them. Any thoughts? <?php $ads = array ( 'ad-stt-weddings-elisha-orin-photography', 'ad-stt-weddings-brandi-mays-videographer', 'ad-stt-weddings-sugar-and-spice-artistry', 'ad-stt-weddings-weddings-the-island-way'); shuffle($ads); foreach ( $ads as $ad ) { WriteContent($ad); } Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023030 Share on other sites More sharing options...
sasa Posted March 8, 2010 Share Posted March 8, 2010 try <?php $ads = array ( 'ad-stt-weddings-elisha-orin-photography', 'ad-stt-weddings-brandi-mays-videographer', 'ad-stt-weddings-sugar-and-spice-artistry', 'ad-stt-weddings-weddings-the-island-way'); shuffle($ads); $count = 0; $max = 2; foreach ( $ads as $ad ) { if ($count++ >= $max) break; WriteContent($ad); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023035 Share on other sites More sharing options...
ksimpkins Posted March 8, 2010 Author Share Posted March 8, 2010 Thats perfect! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023040 Share on other sites More sharing options...
Psycho Posted March 8, 2010 Share Posted March 8, 2010 use foreach($ads as $ads) echo $ads; To echo the code after shuffle function. Really? That would produce an error after the first iteration of the loop. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023079 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.