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
https://forums.phpfreaks.com/topic/193115-php-shuffle-array/
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
https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1016988
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
https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023030
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
https://forums.phpfreaks.com/topic/193115-php-shuffle-array/#findComment-1023035
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.