Code: Select all
$myArray = array(0,1,2,3,4);
for($i = 0, $j = count($myArray); $i < $j; $i++)
{
foreach($myArray as $k => $v)
{
if($i < $k)
{
$possibles[] = array_diff($myArray, array($myArray[$i],$myArray[$k]));
}
}
}Any ideas?
Moderator: General Moderators
Code: Select all
$myArray = array(0,1,2,3,4);
for($i = 0, $j = count($myArray); $i < $j; $i++)
{
foreach($myArray as $k => $v)
{
if($i < $k)
{
$possibles[] = array_diff($myArray, array($myArray[$i],$myArray[$k]));
}
}
}Code: Select all
$input = array(0,1,2);
$output = array(
array(0,1,2)
);Code: Select all
$input = array(0,1,2,3);
$output = array(
array(0,1,2),
array(0,1,3),
array(0,2,3),
array(1,2,3)
);Code: Select all
$input = array(0,1,2,3,4);
$output = array(
array(0,1,2),
array(0,1,3),
array(0,1,4),
array(0,2,3),
array(0,2,4),
array(0,3,4),
array(1,2,3),
array(1,2,4),
array(1,3,4),
array(2,3,4)
);Code: Select all
function returnAllPossibilities($array)
{
$possibilities = array();
$count = count($array);
while (count($possibilities) <= ($count * $count))
{
shuffle($array);
if (!in_array($temp, $possibilites))
{
$possibilities[] = $temp;
}
}
return $possibilities;
}Maybe I am missing your direction on this but I was going to put them all in one multi dimensional array. What's the problem there?Mordred wrote:If you really need to walk through all combinations, by all means do NOT generate them in advance in multiple arrays, use a callback while generating them.
That's scary!!! It's worse than brute force!!! I'm not sure where temp is coming from but this line:Jcart wrote:I can't quite think of a good way to do this.. so I came up with a bad way to (likely going to waste many cycles)Code: Select all
function returnAllPossibilities($array) { $possibilities = array(); $count = count($array); while (count($possibilities) <= ($count * $count)) { shuffle($array); if (!in_array($temp, $possibilites)) { $possibilities[] = $temp; } } return $possibilities; }
Code: Select all
while (count($possibilities) <= ($count * $count))Code: Select all
while(10 <= 25){Code: Select all
<?php
function returnComb($anArr)
{
$retArr = array();
$Cnt = count($anArr);
for($i = 0; $i < $Cnt-2; $i++)
for($j = $i+1; $j < $Cnt-1; $j++)
for($k = $j+1; $k < $Cnt; $k++)
$retArr[] = array($i, $j, $k);
return $retArr;
}
print '<pre>'.print_r(returnComb(array(0, 1, 2, 3, 4)), TRUE).'</pre>';
?>
php.net/shuffle wrote: This is taken from an O'Reilly Book and modified slightly to return all possible permutations of a 1D array:
Code: Select all
# 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);
You are getting more than ten because you have added order as a consideration.scottayy wrote:There's more than 10 possible combos off the top of my head.
Code: Select all
function combs_less_one($arr) {
$ret = array();
foreach(array_reverse(array_keys($arr)) as $key) {
$ret[] = array_merge(array_diff($arr, array($arr[$key])));
}
return $ret;
}
function combs_of_size($arr, $size) {
$steps = count($arr)-$size;
$list = array($arr);
for($i=0;$i<$steps;++$i) {
$tmp = array();
foreach($list as $curlist) {
$tmp = array_merge($tmp,combs_less_one($curlist));
}
$list = $tmp;
}
// make unique, array_unique does not work because they are arrays
$ret = array();
foreach ($list as $comb) {
if (!in_array($comb,$ret)) $ret[] = $comb;
}
return $ret;
}