note 99624 modified in function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Fri, 27 Aug 2010 11:04:22 +0000 |
| Subject: | note 99624 modified in function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172109@lists.php.net to get a copy of this message | ||
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the
shuffled array instead of shuffling it in place.
<?php
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $list[$key];
return $random;
}
?>
--was--
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the
shuffled array instead of shuffling it in place.
<?
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $list[$key];
return $random;
}
?>
http://php.net/manual/en/function.shuffle.php