IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

EDI, CMS, Outils, Scripts et API PHP Discussion :

combinaison de tableau


Sujet :

EDI, CMS, Outils, Scripts et API PHP

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2009
    Messages : 4
    Points : 2
    Points
    2
    Par défaut combinaison de tableau
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    header("Cache-Control: no-cache");
    header("Pragma: nocache");
     
    require('_config-rating.php');
     
    //création des valeur
    $vote_sent = preg_replace("/[^0-9]/","",$_REQUEST['j']);
    $id_sent = preg_replace("/[^0-9a-zA-Z-]/","",$_REQUEST['q']);
    $ip_num = preg_replace("/[^0-9\.]/","",$_REQUEST['t']);
    $units = preg_replace("/[^0-9]/","",$_REQUEST['c']);
    $ip = $_SERVER['REMOTE_ADDR'];
     
    // suprimer le script parce que les utilisateurs normaux ne le verront jamais.
    if ($vote_sent > $units) die("Désolé, le vote a l'air d'être sans fondement.");

  2. #2
    Expert éminent
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Points : 7 762
    Points
    7 762
    Par défaut
    J'ai une fonction qui calcule le produit cartésien de plusieurs tableau et génère l'intégralité des combinaisons possibles entre tous les éléments de tous les tableaux.

    Si c'est effectivement ce que tu cherches, tu peux l'utiliser:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    function array_cartesian_product () {
      if (!$c = func_num_args()) return array();
      if ($c == 1) {
        $r = array();
    	foreach (func_get_arg(0) as $v) {
    		$r[] = array($v);
    	}
    	return $r;
      }
     
      $args = func_get_args();
      $f = array_shift($args);
      $s = call_user_func_array('array_cartesian_product', $args);
      $r = array();
     
      foreach ($f as $v) {
        foreach ($s as $w) {
    	  array_unshift($w, $v);
    	  $r[] = $w;
    	}
      }
     
      return $r;
    }
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    $a = array(1, 5, 4, 3);
    $b = array(2, 3);
    $c = array(6, 7, 8, 9, 10);
     
    var_dump(array_cartesian_product($a,$b,$c));
    Résultat:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    array
      0 => 
        array
          0 => int 1
          1 => int 2
          2 => int 6
      1 => 
        array
          0 => int 1
          1 => int 2
          2 => int 7
      2 => 
        array
          0 => int 1
          1 => int 2
          2 => int 8
      3 => 
        array
          0 => int 1
          1 => int 2
          2 => int 9
      4 => 
        array
          0 => int 1
          1 => int 2
          2 => int 10
      5 => 
        array
          0 => int 1
          1 => int 3
          2 => int 6
      6 => 
        array
          0 => int 1
          1 => int 3
          2 => int 7
      7 => 
        array
          0 => int 1
          1 => int 3
          2 => int 8
      8 => 
        array
          0 => int 1
          1 => int 3
          2 => int 9
      9 => 
        array
          0 => int 1
          1 => int 3
          2 => int 10
      10 => 
        array
          0 => int 5
          1 => int 2
          2 => int 6
      11 => 
        array
          0 => int 5
          1 => int 2
          2 => int 7
      12 => 
        array
          0 => int 5
          1 => int 2
          2 => int 8
      13 => 
        array
          0 => int 5
          1 => int 2
          2 => int 9
      14 => 
        array
          0 => int 5
          1 => int 2
          2 => int 10
      15 => 
        array
          0 => int 5
          1 => int 3
          2 => int 6
      16 => 
        array
          0 => int 5
          1 => int 3
          2 => int 7
      17 => 
        array
          0 => int 5
          1 => int 3
          2 => int 8
      18 => 
        array
          0 => int 5
          1 => int 3
          2 => int 9
      19 => 
        array
          0 => int 5
          1 => int 3
          2 => int 10
      20 => 
        array
          0 => int 4
          1 => int 2
          2 => int 6
      21 => 
        array
          0 => int 4
          1 => int 2
          2 => int 7
      22 => 
        array
          0 => int 4
          1 => int 2
          2 => int 8
      23 => 
        array
          0 => int 4
          1 => int 2
          2 => int 9
      24 => 
        array
          0 => int 4
          1 => int 2
          2 => int 10
      25 => 
        array
          0 => int 4
          1 => int 3
          2 => int 6
      26 => 
        array
          0 => int 4
          1 => int 3
          2 => int 7
      27 => 
        array
          0 => int 4
          1 => int 3
          2 => int 8
      28 => 
        array
          0 => int 4
          1 => int 3
          2 => int 9
      29 => 
        array
          0 => int 4
          1 => int 3
          2 => int 10
      30 => 
        array
          0 => int 3
          1 => int 2
          2 => int 6
      31 => 
        array
          0 => int 3
          1 => int 2
          2 => int 7
      32 => 
        array
          0 => int 3
          1 => int 2
          2 => int 8
      33 => 
        array
          0 => int 3
          1 => int 2
          2 => int 9
      34 => 
        array
          0 => int 3
          1 => int 2
          2 => int 10
      35 => 
        array
          0 => int 3
          1 => int 3
          2 => int 6
      36 => 
        array
          0 => int 3
          1 => int 3
          2 => int 7
      37 => 
        array
          0 => int 3
          1 => int 3
          2 => int 8
      38 => 
        array
          0 => int 3
          1 => int 3
          2 => int 9
      39 => 
        array
          0 => int 3
          1 => int 3
          2 => int 10

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2009
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Merci
    J'ai 3 tables MySQl :
    inscrits_tb => id_inscrits, nom, prenom, etc...
    categorie_tb => id_categorie, intitule
    inscat_tb => id_inscat, id_inscrit, id_catégorie

  4. #4
    Expert éminent
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Points : 7 762
    Points
    7 762
    Par défaut
    Pas de quoi.

    J'en ai profité pour me replonger dedans et l'améliorer, voici donc la dernière version:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    function array_cartesian_product () {
        if (!$c = func_num_args())
            return array();
     
        if ($c == 1) {
            foreach ((array)func_get_arg(0) as $v)
                $r[] = (array)$v;
            return $r;
        }
     
        $a = func_get_args();
        $f = array_shift($a);
        $s = call_user_func_array(__FUNCTION__, $a);
     
        foreach ((array)$f as $v) {
            foreach ($s as $w) {
                array_unshift($w, $v);
                $r[] = $w;
            }
        }
     
        return $r;
    }
    Elle permet désormais l'utilisation de types scalaires, ce qui est bien pratique. Voici un exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    $couleurs = array('pique', 'trefle', 'coeur ', 'carreau');
     
    $cartes = array(2,3,4,5,6,6,7,8,9,10,'valet','dame','roi','as');
     
    $paquet = array_map('implode', array_cartesian_product($cartes, ' de ', $couleurs));
     
    shuffle($paquet);
     
    echo "Première carte tirée: " . array_shift($paquet) . '<br />';
    echo "Deuxième carte tirée: " . array_shift($paquet) . '<br />';
    echo "Troisième carte tirée: " . array_shift($paquet) . '<br />';

Discussions similaires

  1. [MySQL] pchart combinaison impossible tableau avec graphique
    Par Jarod51 dans le forum PHP & Base de données
    Réponses: 0
    Dernier message: 01/09/2011, 13h30
  2. Réponses: 2
    Dernier message: 11/02/2007, 17h13
  3. trouver les combinaisons possibles d'un tableau ?
    Par titoumimi dans le forum Algorithmes et structures de données
    Réponses: 12
    Dernier message: 20/09/2006, 20h29
  4. Sortir d'un tableau les combinaisons possibles
    Par juelo dans le forum Algorithmes et structures de données
    Réponses: 33
    Dernier message: 26/03/2006, 17h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo