Advertisement
imoda

All combinations of strings

Sep 14th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2.  
  3.     function all($array, $partial, &$result) {
  4.        
  5.         if ($array == array()) {
  6.            
  7.             $result[] = implode(',', $partial);
  8.             return;
  9.         }
  10.         for ($i = 0; $i < count($array); $i++) {
  11.            
  12.             $e = $array[$i];
  13.             $a = $array;
  14.            
  15.             array_splice($a, $i, 1);
  16.            
  17.             foreach($e as $v) {
  18.                
  19.                 $p = $partial;
  20.                 $p[] = $v;
  21.                
  22.                 all($a, $p, $result);
  23.             }
  24.         }
  25.     }
  26.    
  27.     $a = array(1, 2, 3);
  28.     $b = array('foo', 'bar');
  29.     $c = array('a', 'b');
  30.     $params = array($a, $b, $c);
  31.    
  32.     $result = array();
  33.    
  34.     all($params, array(), $result);
  35.    
  36.     print_r($result);
  37.    
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement