Page 1 sur 1

question sur les tableaux

Posté : 13 mai 2011, 20:43
par light86
bonsoir j'ai ce tableau $row

Code : Tout sélectionner

0 => string 'fdfd_13_05_2011_19_18_20' (length=24) 'nomprojet' => string 'fdfd_13_05_2011_19_18_20' (length=24) 1 => string 'admin' (length=5) 'auteur' => string 'admin' (length=5) 2 => string 'nomappel' (length=8) 'element1' => string 'nomappel' (length=8) 3 => string 'fdfd' (length=4) 'valeur1' => string 'fdfd' (length=4) 4 => string 'jour' (length=4) 'element2' => string 'jour' (length=4) 5 => string '00' (length=2) 'valeur2' => string '00' (length=2) 6 => string 'mois' (length=4) 'element3' => string 'mois' (length=4) 7 => string '00' (length=2) 'valeur3' => string '00' (length=2)
ma question comment je peut avoir un tableau associatif $ass commme suit
lire la clé du $row et la valeur de cette clé est la valeur de la prochaine clé
exemple
$ass[element1]=$row[3]
$ass=[nomprojet]=row[1]
merci

Re: question sur les tableaux

Posté : 13 mai 2011, 21:45
par Ryle
Ton tableau serait sans doute plus simple à traiter s'il était uniquement associatif (et pas associatif et indexé), mais voici déjà un début de piste...
$ass = array();
$i = 0; // compteur
foreach ($row as $key => value) { // pour chaque élément du tableau
  if (!is_int($key)) { // si la clé n'est pas numérique
    $ass[$key] = $row[$i++]; // alimente ton tableau et incrémente l'index
  }
}
C'est fait rapidement, il te faut compléter pour gérer le dernier index qui n'aura pas de valeur, et savoir ce que tu fais de la première valeur qui du coup n'aura pas d'index...

Re: question sur les tableaux

Posté : 13 mai 2011, 21:49
par light86
slt voici le post original

Code : Tout sélectionner

'nomappel' => string 'fsdfdsdf' (length=8) 'jour' => string '00' (length=2) 'mois' => string '00' (length=2) 'anne' => string '' (length=0) 'nombre' => string '2' (length=1) 'type' => string 'pnr' (length=3) 'envoye' => string 'Envoyer' (length=7) 'date' => string '00/00/' (length=6) 'element_1' => string 'tableau' (length=7) 'element_2' => string 'tableau' (length=7) 'tableau1_nombre' => string '2' (length=1) 'tableau2_nombre' => string '2' (length=1) 'titretableau1' => string 'FDS' (length=3) 'tableau1_11' => string 'FDS' (length=3) 'tableau1_12' => string '' (length=0) 'tableau1_21' => string 'FDS' (length=3) 'tableau1_22' => string '' (length=0) 'titretableau2' => string 'FSD' (length=3) 'tableau2_11' => string 'FSD' (length=3) 'tableau2_12' => string '' (length=0) 'tableau2_21' => string 'SDF'
je recoit des deux parce j'ai fait
$sql="select * from projet2 where auteur='admin' and

$row = mysql_fetch_array($res, MYSQL_BOTH);
mon but avoir une variable $ass qui a la même structure que le $_POST
merci