note 62840 added to function.shuffle
| From: | alejandro dot garza at itesm dot mx | Date: | Fri, 10 Mar 2006 16:47:58 +0000 |
| Subject: | note 62840 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105218@lists.php.net to get a copy of this message | ||
This is taken from an O'Reilly Book and modified slightly to return all possible permutations
of a 1D array:
<?php
# Modified from http://linuxalpha1.eicn.ch/OReilly_books/
books/webprog/pcook/ch04_26.htm
# Takes a non-associatuve 1D (vector) array of items
# and returns an array of arrays with each possible permutation
function array_2D_permute($items, $perms = array( )) {
static $permuted_array;
if (empty($items)) {
$permuted_array[]=$perms;
#print_r($new);
#print join(' ', $perms) . "\n";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
array_2D_permute($newitems, $newperms);
}
return $permuted_array;
}
}
$arr=array("Architecture","Mexico","Periodicals","Test");
$result=array_2D_permute($arr);
print_r($result);
?>
----
Server IP: 216.194.113.175
Probable Submitter: 131.178.5.45
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- http://master.php.net/note/edit/62840
Del: integrated -- http://master.php.net/note/delete/62840/integrated
Del: useless -- http://master.php.net/note/delete/62840/useless
Del: bad code -- http://master.php.net/note/delete/62840/bad+code
Del: spam -- http://master.php.net/note/delete/62840/spam
Del: non-english -- http://master.php.net/note/delete/62840/non-english
Del: in docs -- http://master.php.net/note/delete/62840/in+docs
Del: other reasons-- http://master.php.net/note/delete/62840
Reject -- http://master.php.net/note/reject/62840
Search -- http://master.php.net/manage/user-notes.php