Jump to content

PHP Shuffle Array


ksimpkins

Recommended Posts

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);

?>

Link to comment
Share on other sites

<?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 ";
}
?>

 

Link to comment
Share on other sites

  • 2 weeks later...

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);

}

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.