PHP Shuffle Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ksimpkins
Forum Newbie
Posts: 2
Joined: Tue Feb 23, 2010 1:03 pm

PHP Shuffle Array

Post by ksimpkins »

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);
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Shuffle Array

Post by requinix »

Congratulations! You won a prize for not telling us what you need help with!

You can find your prize here.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Shuffle Array

Post by McInfo »

I'm just guessing that this is the problem: PHP can't change page content after it has been sent to the browser. In other words, you can't shuffle the ads if they have already been written.

This might work; however, it is unclear how the "ad-..." strings influence your application and what the WriteContent() function does.

Code: Select all

<?php
$ads = array ('ad-one', 'ad-two');
shuffle($ads);
foreach ($ads as $ad) {
    WriteContent($ad);
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 5:16 pm, edited 1 time in total.
ksimpkins
Forum Newbie
Posts: 2
Joined: Tue Feb 23, 2010 1:03 pm

Re: PHP Shuffle Array

Post by ksimpkins »

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);
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Shuffle Array

Post by AbraCadaver »

This?

Code: Select all

shuffle($ads);
for($i=1; $i<=5; $i++) {
   WriteContent($ads[$i]);
}
Or...

Code: Select all

$rand_ads = array_rand($ads, 5);
foreach($rand_ads as $ad) {
       WriteContent($ads[$ad]);
}
Or...

Code: Select all

$rand_ads = array_rand(array_flip($ads), 5);
foreach($rand_ads as $ad) {
       WriteContent($ad);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply