Page 1 sur 1

Remplir de 0 un tableau à 2 dimensions

Posté : 25 juil. 2007, 11:33
par Ish
Bonjour tous le monde,

je voudrais remplir un tableau à 2 dimensions avec que des 0 ...
Voici mon code mais ça ne marche pas !!

Code : Tout sélectionner

$resData = array () ; for ($z ; $z < 23 ; $z++) { for ($w ; $w < 13 ; $w++) { $resData[$z][] = 0 ; } }

Posté : 25 juil. 2007, 11:35
par Genova
Faudrait penser à initialiser tes variables
$resData = array () ;
for ($z = 0; $z < 23 ; $z++) {
  for ($w = 0; $w < 13 ; $w++) {
    $resData[$z][] = 0 ;
  }
} 

Posté : 25 juil. 2007, 11:40
par Ish
oula la la ..... ça c'est grave !!!

j'avais trouver entre temps cela !!

Code : Tout sélectionner

$resData = array(); $array12 = array(); for ($f = 0; $f < 12;$f++) {$array12[] = '&nbsp;';} for ($f = 0; $f < 23;$f++) {$resData[] = $array12;}
Merci

Posté : 25 juil. 2007, 15:48
par Sékiltoyai
Sinon, un truc comme ca, ca marche aussi :
$tab = array();
for($i=0; $i<$nb_lines, $i++) $tab[]=array_fill(0, $nb_cols, 0);
A voir après niveau performances la meilleure méthode...

Posté : 25 juil. 2007, 17:37
par Hubert Roksor
Faudrait penser à initialiser tes variables
Perso, un aussi bon conseil je le mettrais en gras et en rouge. D'ailleurs je ne m'en prive pas :

Faudrait penser à initialiser tes variables :lol:

@Ish: ajoute ça en début de script si tu veux moins te casser la tête pour trouver les erreurs dans ton code
error_reporting(E_ALL);
un truc comme ca, ca marche aussi
Et si ça marche, pourquoi s'arrêter en si bon chemin ?
$z = 23;
$w = 13;

$resData = array_fill(0, $z, array_fill(0, $w, 0));
PS: la gestion de tableaux multidimensionnels est plutôt compliquées, si tu as d'autres bugs songe sérieusement à revoir ton design

Posté : 26 juil. 2007, 15:04
par Sékiltoyai
un truc comme ca, ca marche aussi
Et si ça marche, pourquoi s'arrêter en si bon chemin ?
Parce qu'il fallait y penser :)