array_unique

ViPHP
ViPHP | 2291 Messages

10 oct. 2008, 19:12

Bonjour,
Je crée un tableau avec trois champs de ma table les champs comporte des chiffres
exemple:
3,6,9
9,3,0
7,9,15
etc...
Mon code:
while($row = mysql_fetch_assoc($result)){
$total = $row['total'];

              $a = array($row['equa1']);
              $b = array($row['equa2']);
              $c = array($row['equa3']);

              $e = array_merge($a, $b, $c);//fusionne les tableaux             
              $db = array_unique($e);



 # on peut utiliser une requete SQL pour alimenter le tableau
 $camembert->add_tab ($total, $db[0]." Db");
 $camembert->add_tab ($total, $db[1]." Db");
 $camembert->add_tab ($total, $db[2]." Db");
Mon soucis es tque je n'arrive pas a faire en sorte qu'une valeur s'affiche pas plusieurs fois
Merci d'avance

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 13231 Messages

10 oct. 2008, 19:35

Je ne comprend pas trop là ...
Avec le code suivant :
<?php

$array1 = array('a','b','c','d','e','f');
$array2 = array('c','e','g','j','a','m');
$array3 = array('b','z','f','k','x','c');

$array_complet = array_merge($array1, $array2, $array3);
$array_unique = array_unique($array_complet);

echo "tableau complet :";
echo "<pre>"; var_dump($array_complet); echo "</pre>"; 
echo "<pre>"; var_dump($array_unique); echo "</pre>"; 
J'obtiens le résultat suivant :

Code : Tout sélectionner

tableau complet : array(18) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [6]=> string(1) "c" [7]=> string(1) "e" [8]=> string(1) "g" [9]=> string(1) "j" [10]=> string(1) "a" [11]=> string(1) "m" [12]=> string(1) "b" [13]=> string(1) "z" [14]=> string(1) "f" [15]=> string(1) "k" [16]=> string(1) "x" [17]=> string(1) "c" } array(12) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [8]=> string(1) "g" [9]=> string(1) "j" [11]=> string(1) "m" [13]=> string(1) "z" [15]=> string(1) "k" [16]=> string(1) "x" }
Est-ce que tu peux essayer de faire des var_dump() sur tes tableaux temporaires, puis sur $e et $db ?
Connaître son ignorance est la meilleure part de la connaissance
Pour un code lisible : n'hésitez pas à sauter des lignes et indenter

twitter - site perso - Github - Zend Certified Engineer

ViPHP
ViPHP | 2291 Messages

10 oct. 2008, 20:43

Salut :wink:

L'idée est de créer un tableau avec mes trois champs.
$a = array($row['equa1']);
$b = array($row['equa2']);
$c = array($row['equa3']);
Donc avec mes trois tableau je les fusionne pour n'en avoir qu'un
$e  = array_merge($a, $b, $c);
jusque là pas de soucis
après j'essaye de supprimer les doublons
array_unique
Mais ca ne fonctionne pas :?:

Mes VAR_DUMP
$a
array(1) {
[0]=>
string(1) "0"
}
array(1) {
[0]=>
string(0) ""
}
array(1) {
[0]=>
string(2) "15"
}
array(1) {
[0]=>
string(2) "15"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(2) "12"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(1) "6"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(2) "15"
}
array(1) {
[0]=>
string(1) "6"
}
$b
array(1) {
[0]=>
string(1) "0"
}
array(1) {
[0]=>
string(0) ""
}
array(1) {
[0]=>
string(1) "3"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(1) "6"
}
array(1) {
[0]=>
string(2) "24"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(1) "6"
}
array(1) {
[0]=>
string(1) "9"
}
array(1) {
[0]=>
string(1) "3"
}
array(1) {
[0]=>
string(1) "6"
}
array(1) {
[0]=>
string(1) "3"
}
et la même chose pour $c
$e
array(3) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "9"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "12"
[1]=>
string(2) "24"
[2]=>
string(2) "31"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "9"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "6"
[1]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "9"
[2]=>
string(1) "3"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "6"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
et $db
array(1) {
[0]=>
string(1) "0"
}
array(1) {
[0]=>
string(0) ""
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "9"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "12"
[1]=>
string(2) "24"
[2]=>
string(2) "31"
}
array(2) {
[0]=>
string(1) "9"
[2]=>
string(1) "0"
}
array(2) {
[0]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(2) {
[0]=>
string(1) "9"
[2]=>
string(1) "3"
}
array(3) {
[0]=>
string(1) "9"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(2) "15"
[1]=>
string(1) "6"
[2]=>
string(1) "0"
}
array(3) {
[0]=>
string(1) "6"
[1]=>
string(1) "3"
[2]=>
string(1) "0"
}
Le but de de sortir le nombre de 15,6,9 etc...
je ne suis pas doué avec les tableau mais je ne comprend pas mon erreur :!:
Ma démarche est peut-être mauvaise aussi :lol:

Modérateur PHPfrance
Modérateur PHPfrance | 2575 Messages

10 oct. 2008, 23:07

Bonjour,

Mon code:
while($row = mysql_fetch_assoc($result)){
$total = $row['total'];

              $a = array($row['equa1']);
              $b = array($row['equa2']);
              $c = array($row['equa3']);

              $e = array_merge($a, $b, $c);//fusionne les tableaux             
              $db = array_unique($e);



 # on peut utiliser une requete SQL pour alimenter le tableau
 $camembert->add_tab ($total, $db[0]." Db");
 $camembert->add_tab ($total, $db[1]." Db");
 $camembert->add_tab ($total, $db[2]." Db");
Selon ton code et les dumps que tu as posté, je constate que les variables $a, $b et $c ne sont pas des tableaux. La preuve est que leurs indexes sont tous à [0] dans le dump et que tu leurs affectent un champ à la fois dans la boucle While de la boucle.
En plus, il est logiquement faux que tu mettes les traitements array_merge() et array_unique() dans la boucle while.
Il fallait affecter les champs dans les tableaux $a, $b et $c dans le while et attendre la fin de la boucle pour traiter les tableaux correctement obtenus.

Proposition de correction:
// Initilisation des tableaux
$a = array(); $b = array(); $c = array();

// Remplissage des tableaux
while($row = mysql_fetch_assoc($result)){
   $total = $row['total'];
   // Ajout des valeurs des champs dans les tableaux
   array_push($a, $row['equa1']); 
   array_push($b, $row['equa2']);
   array_push($c, $row['equa3']);
}

// Traitement des tableaux remplis
$e = array_merge($a, $b, $c);    // Fusion
$db = array_unique($e);             // Épuration des doublons

// Affichage des résultats
echo "Contenu des tableaux : <ul><pre>";
print_r($a); 
print_r($b);
print_r($c);
print_r($db); 
echo "</pre></ul>";
--------//////----//---//----//////
-------//---//----//---//----//---//
------//////----//////-----//////
-----||--------||--||---||
Prendre le recul n'est pas une perte de temps.


ps: Affrontez moi dans l'arène

ViPHP
ViPHP | 2291 Messages

11 oct. 2008, 19:31

Merci.
:oops: :P
Merci.