note 88408 added to function.shuffle

From: Date: Thu, 22 Jan 2009 18:06:30 +0000
Subject: note 88408 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-149669@lists.php.net to get a copy of this message
If you want the Power Set (set of all unique subsets) of an array instead of permutations, you can use this simple algorithm: /** * Returns the power set of a one dimensional array, * a 2-D array. * array(a,b,c) -> * array(array(a),array(b),array(c),array(a,b),array(b,c),array(a,b,c)) */ function powerSet($in,$minLength = 1) { $count = count($in); $members = pow(2,$count); $return = array(); for ($i = 0; $i < $members; $i++) { $b = sprintf("%0".$count."b",$i); $out = array(); for ($j = 0; $j < $count; $j++) { if ($b{$j} == '1') $out[] = $in[$j]; } if (count($out) >= $minLength) { $return[] = $out; } } return $return; } ---- Server IP: 208.69.120.35 Probable Submitter: 207.38.19.83 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/88408 Del: integrated -- https://master.php.net/note/delete/88408/integrated Del: useless -- https://master.php.net/note/delete/88408/useless Del: bad code -- https://master.php.net/note/delete/88408/bad+code Del: spam -- https://master.php.net/note/delete/88408/spam Del: non-english -- https://master.php.net/note/delete/88408/non-english Del: in docs -- https://master.php.net/note/delete/88408/in+docs Del: other reasons-- https://master.php.net/note/delete/88408 Reject -- https://master.php.net/note/reject/88408 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149669) next »