note 96956 modified in function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Wed, 24 Mar 2010 22:20:46 +0000 |
| Subject: | note 96956 modified in function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167370@lists.php.net to get a copy of this message | ||
Many people in SEO need to supply an array and shuffle the results and need the same result each
time that page is generated. This is my implementation with a working example:
<?php
function seoShuffle(&$items,$string) {
mt_srand(strlen($string));
for ($i = count($items) - 1; $i > 0; $i--){
$j = @mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
$items =
array('one','two','three','four','five','six');
$string = 'whatever';
echo '<pre>';
print_r($items);
echo '</pre>';
seoShuffle($items,$string);
echo '<pre>';
print_r($items);
echo '</pre>';
?>
--was--
Many people in SEO need to supply an array and shuffle the results and need the same result each
time that page is generated. This is my implementation with a working example:
<?
function seoShuffle(&$items,$string) {
mt_srand(strlen($string));
for ($i = count($items) - 1; $i > 0; $i--){
$j = @mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
$items =
array('one','two','three','four','five','six');
$string = 'whatever';
echo '<pre>';
print_r($items);
echo '</pre>';
seoShuffle($items,$string);
echo '<pre>';
print_r($items);
echo '</pre>';
?>
http://php.net/manual/en/function.shuffle.php