note 62840 modified in function.shuffle by danbrown

From: Date: Fri, 25 Jul 2008 17:48:22 +0000
Subject: note 62840 modified in function.shuffle by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142721@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); ?> --was-- 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); ?> http://php.net/manual/en/function.shuffle.php

« previous php.notes (#142721) next »