14

arraypowerset

array_power_set
SYNOPSIS

array_power_set($arr)

DESCRIPTION

array_power_set retourne un tableau de tous les différents tableaux qui peuvent être fabriqués avec aucun, tous les éléments ou une partie des éléments de $arr. Si $arr est vide, array_power_set retourne un tableau contenant un tableau vide.

EXEMPLE
php > require 'library/dump.php';
php > require 'library/arraypowerset.php';
php > $arr=array(1, 2, 3);
php > dump(array_power_set($arr));
array(8) {
  [0] => array(0) {
  }
  [1] => array(1) {
    [0] => int(1)
  }
  [2] => array(1) {
    [0] => int(2)
  }
  [3] => array(2) {
    [0] => int(2)
    [1] => int(1)
  }
  [4] => array(1) {
    [0] => int(3)
  }
  [5] => array(2) {
    [0] => int(3)
    [1] => int(1)
  }
  [6] => array(2) {
    [0] => int(3)
    [1] => int(2)
  }
  [7] => array(3) {
    [0] => int(3)
    [1] => int(2)
    [2] => int(1)
  }
}

arraypowerset(array(1,2,3)) retourne array(array(), array(1), array(2), array(3), array(1, 2), array(1, 3), array(2, 3), array(1, 2, 3)).

CODE
  1. function array_power_set($arr) {
  2.     $r = array(array( ));
  3.  
  4.     foreach ($arr as $e) {
  5.         foreach ($r as $c) {
  6.             array_push($r, array_merge(array($e), $c));
  7.         }
  8.     }
  9.  
  10.     return $r;
  11. }

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].