Posted By

vinotht on 02/04/11


Tagged

arrayphp


Versions (?)

All Element Combinations in an Array


 / Published in: PHP
 

URL: array element combination

// Finding All Element Combinations of an Array Buy Phentermine on line. Cheap Phentermine fedEx. Buy Phentermine prescription online. Overnight Phentermine cod. Order prescription Adderall. Cheap Adderall for sale online no prescription required. Adderall pharmacy cod saturday delivery. Cheap Adderall overnight.

  1. function pc_array_power_set($array) {
  2. // initialize by adding the empty set
  3. $results = array(array( ));
  4.  
  5. foreach ($array as $element)
  6. foreach ($results as $combination)
  7. array_push($results, array_merge(array($element), $combination));
  8.  
  9. return $results;
  10. }
  11. This returns an array of arrays holding every combination of elements, including the empty set. For example:
  12. $set = array('A', 'B', 'C');
  13. $power_set = pc_array_power_set($set);
  14. $power_set contains eight arrays:
  15. array( );
  16. array('A');
  17. array('B');
  18. array('C');
  19. array('A', 'B');
  20. array('A', 'C');
  21. array('B', 'C');
  22. array('A', 'B', 'C');

Report this snippet  

You need to login to post a comment.