0

already look around but cant find what i want for PHP.

just say i have a number : 1234 ( can be splitted first into array ) and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits

for example :

possible 4 digits will be : 1234,1243,1324,1342, and so on. ( i dont know how many more )

possible 2 digits will be : 12,13,14,21,23,24,31,32,34,41,42,43

the closest one i get is :

$p = permutate(array('1','2','3','4'));
$result = array();
foreach($p as $perm) {
   $result[]=join("",$perm);
}
$result = array_unique($result);
print join("|", $result);

function permutate($elements, $perm = array(), &$permArray = array()){
    if(empty($elements)){
       array_push($permArray,$perm); return;
    }

    for($i=0;$i<=count($elements)-1;$i++){
       array_push($perm,$elements[$i]);
       $tmp = $elements; array_splice($tmp,$i,1);
       permutate($tmp,$perm,$permArray);
       array_pop($perm);
    }

    return $permArray;
}

but how can i edit this so i can display for 3 and 2 digits ?

Thanks

dognose
  • 20,360
  • 9
  • 61
  • 107
Davin Kho
  • 13
  • 3
  • `12` and `21` should be treated as different values, right? – Aleksei Matiushkin Feb 11 '15 at 17:29
  • 3
    Do you really just want to know the _number_ of permutations, or do you need to output all of them? There is a [simple formula](http://en.wikipedia.org/wiki/Permutation#k-permutations_of_n) for the _number_ of permutations. – matt Feb 11 '15 at 17:31
  • http://php.net/manual/en/function.shuffle.php#90615 — the permutation impl in PHP. – Aleksei Matiushkin Feb 11 '15 at 17:32
  • possible duplicate of [Permutations - all possible sets of numbers](http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers) – matt Feb 11 '15 at 17:35
  • @mudasobwa yes its should be treated as different values – Davin Kho Feb 12 '15 at 01:25
  • @mudasobwa and yes im try to edit those function so it can fit what i want. but i think thats it. – Davin Kho Feb 12 '15 at 01:40

1 Answers1

0

i got what i want

it's from @mudasobwa link. and i edit to what i want.

<?php
$in = array(1,2,3,4,5,6);
  $te = power_perms($in);

 // print_r($te);
$thou=0;
$hun =0;
$pu = 0;
for($i=0;$i<count($te);$i++)
{
$jm = count($te[$i]);

    for($j=0;$j<$jm;$j++)
    {
    $hsl[$i] = $hsl[$i] . $te[$i][$j];
    }

    if($hsl[$i] >=100 && $hsl[$i] < 1000 )
    {
    $ratus[$hun] =  intval($hsl[$i]);
    $hun = $hun + 1;
    }

    if($hsl[$i] <100 && $hsl[$i] >=10)
    {
    $pul[$pu] = intval($hsl[$i]);
    $pu = $pu + 1;
    }

    if($hsl[$i] >=1000 && $hsl[$i] < 10000)
    {
    $th[$thou] = intval($hsl[$i]);
    $thou = $thou + 1;
    }
}

$th=array_unique($th);
$pul = array_unique($pul);
$ratus = array_unique($ratus);
sort($ratus);
sort($pul);
sort($th);
print_r($th);



function power_perms($arr) {

    $power_set = power_set($arr);
    $result = array();
    foreach($power_set as $set) {
        $perms = perms($set);
        $result = array_merge($result,$perms);
    }
    return $result;
}

function power_set($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;
      }
   }

//   usort($return,"cmp");  //can sort here by length
   return $return;
}

function factorial($int){
   if($int < 2) {
       return 1;
   }

   for($f = 2; $int-1 > 1; $f *= $int--);

   return $f;
}

function perm($arr, $nth = null) {

    if ($nth === null) {
        return perms($arr);
    }

    $result = array();
    $length = count($arr);

    while ($length--) {
        $f = factorial($length);
        $p = floor($nth / $f);
        $result[] = $arr[$p];
        array_delete_by_key($arr, $p);
        $nth -= $p * $f;
    }

    $result = array_merge($result,$arr);
    return $result;
}

function perms($arr) {
    $p = array();
    for ($i=0; $i < factorial(count($arr)); $i++) {
        $p[] = perm($arr, $i);
    }
    return $p;
}

function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {

    unset($array[$delete_key]);

    if(!$use_old_keys) {
        $array = array_values($array);
    }

    return TRUE;
}
?>
Davin Kho
  • 13
  • 3