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

Langage PHP Discussion :

Echo de tableaux à plusieurs dimensions


Sujet :

Langage PHP

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 28
    Points : 25
    Points
    25
    Par défaut Echo de tableaux à plusieurs dimensions
    Bonjour à tous,
    je bute sur un souci qui doit paraitre évident aux pros du PHP :
    J'ai un tableau de constructeurs.
    Pour chaque constructeur, j'ai un tableau de modèles.
    Pour chaque modèle, j'ai deux tableaux : la gamme et l'unité.
    Je veux afficher le contenu de tout cela dans un echo.
    J'ai essayé ça mais d'une part, je ne suis pas satisfait de mélanger les foreach et les indices $i et $j
    et d'autre part, je n'arrive pas à avoir l'affichage de l'unité (Notice: Array to string conversion) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $i = 0;
    $j = 0;
    foreach ($tConstructeurR as $valueC) {
    	foreach ($tModele[$i] as $valueM) {
    		foreach ($tGamme[$i][$j] as $value) {
    			echo $valueC.'-'.$valueM.'-'.$value.'-'.$tUnite[$i][$j].'<br>';
    		}
    		$j++;
    	}
    	$i++;
    }
    1- Comment faire pour avoir l'unité avec mon code ?
    2- Quelle serait la meilleure façon de faire, pour vous ?

    Merci...

  2. #2
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    OUh la... effectivement ça l'air d'être un peu le bazar dans tes indices.
    Alors déjà, pour te débarrasser des incréments, tu peux utiliser la notation avec clé de foreach : foreach ($tableau as $cle => $valeur).
    Ensuite, je suis un peu perdue dans tes tableaux. Est-ce que tu pourrais donner un exemple de la structure ?
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 28
    Points : 25
    Points
    25
    Par défaut
    j'essaie d'être plus clair.
    La structure :

    constructeur
    modele
    gamme
    unité

    exemple :
    druck
    PDCR22
    35000
    pa
    druck
    PDCR22
    50
    psi

  4. #4
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    Ok. Et tout ça est dans une seule variable (un tableau dans un tableau dans un tableau) ou dans plusieurs (un tableau qui contient les modèles regroupés par constructeur, un qui contient les gammes par modèle...) ?
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  5. #5
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    montre-nous un var_dump() de tes tableaux *.
    (un extrait représentatif suffira !)

    * @Celira
    à priori au moins 4 array : $tConstructeurR, $tModele, $tGamme et $tUnite.

  6. #6
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    @Jreaux : effectivement, c'est ce qu'il semble d'après le code d'origine, mais j'ai du mal à voir comment les tableaux sont reliés entre eux...
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  7. #7
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 28
    Points : 25
    Points
    25
    Par défaut
    Merci de vous intéresser à mon cas.

    Ce sont des tableaux séparés que je "recale" avec les indices mais j'ai un gros doute sur ma "conception"...

    Le dump :

    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
    array (size=2)
      0 => string 'DRUCK' (length=5)
      546 => string 'Digiquartz' (length=10)
    array (size=2)
      0 => 
        array (size=13)
          225 => string 'PDCR10' (length=6)
          233 => string 'PDCR10 10L' (length=10)
          80 => string 'PDCR10 35L' (length=10)
          173 => string 'PDCR10L' (length=7)
          64 => string 'PDCR22' (length=6)
          445 => string 'PDCR32' (length=6)
          0 => string 'PDCR35' (length=6)
          537 => string 'PDCR4010' (length=8)
          534 => string 'PDCR4110' (length=8)
          542 => string 'PDCR4120' (length=8)
          282 => string 'PDCR610' (length=7)
          304 => string 'PDCR820' (length=7)
          512 => string 'PDCR910' (length=7)
      1 => 
        array (size=1)
          0 => string 'Digiquartz' (length=10)
    array (size=2)
      0 => 
        array (size=13)
          0 => 
            array (size=1)
              0 => string '7e+06' (length=5)
          1 => 
            array (size=3)
              1 => string '200000' (length=6)
              0 => string '350000' (length=6)
              3 => string '700000' (length=6)
          2 => 
            array (size=1)
              0 => string '175000' (length=6)
          3 => 
            array (size=1)
              0 => string '1.5e+06' (length=7)
          4 => 
            array (size=9)
              164 => string '7000' (length=4)
              197 => string '17500' (length=5)
              168 => string '35000' (length=5)
              0 => string '70000' (length=5)
              65 => string '105000' (length=6)
              82 => string '175000' (length=6)
              107 => string '350000' (length=6)
              15 => string '700000' (length=6)
              26 => string '3.5e+06' (length=7)
          5 => 
            array (size=2)
              0 => string '175000' (length=6)
              7 => string '350000' (length=6)
          6 => 
            array (size=2)
              0 => string '35000' (length=5)
              10 => string '70000' (length=5)
          7 => 
            array (size=1)
              0 => string '1.35e+07' (length=8)
          8 => 
            array (size=1)
              0 => string '200000' (length=6)
          9 => 
            array (size=1)
              0 => string '1.4e+06' (length=7)
          10 => 
            array (size=2)
              4 => string '1.35e+07' (length=8)
              0 => string '1.5e+07' (length=7)
          11 => 
            array (size=1)
              0 => string '2e+06' (length=5)
          12 => 
            array (size=2)
              0 => string '2e+06' (length=5)
              4 => string '2e+07' (length=5)
      1 => 
        array (size=1)
          0 => 
            array (size=9)
              0 => string '6' (length=1)
              1 => string '15' (length=2)
              8 => string '22' (length=2)
              10 => string '30' (length=2)
              23 => string '45' (length=2)
              43 => string '200' (length=3)
              52 => string '900' (length=3)
              58 => string '1000' (length=4)
              63 => string '3000' (length=4)
    array (size=2)
      0 => 
        array (size=13)
          0 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          1 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          2 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          3 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          4 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          5 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          6 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          7 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          8 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          9 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          10 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          11 => 
            array (size=1)
              0 => string 'Pa' (length=2)
          12 => 
            array (size=1)
              0 => string 'Pa' (length=2)
      1 => 
        array (size=1)
          0 => 
            array (size=1)
              0 => string 'psi' (length=3)

  8. #8
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    Bon, cette fois c'est sûr, il y a un problème de liaison entre les tableaux. A vue de nez, je pense que tu te bases sur l'ordre des insertions dans les tableaux, mais comme les index des tableaux partent dans tous les sens (je suppose que pour les 2 premiers ce doit être des identifiants et non des indices) c'est pas franchement stable.

    Peut-être faudrait-il revoir le bout du script qui créée ces tableaux.
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

Discussions similaires

  1. [PHP 5.0] Tableaux à plusieurs dimensions
    Par Nemesis007 dans le forum Langage
    Réponses: 6
    Dernier message: 10/12/2009, 17h55
  2. Tableaux à plusieurs dimensions
    Par hallaj dans le forum Langage
    Réponses: 2
    Dernier message: 28/06/2008, 14h00
  3. Tableaux à plusieurs dimensions
    Par Cheps dans le forum C++
    Réponses: 2
    Dernier message: 10/03/2008, 13h13
  4. Tableaux à plusieurs dimensions
    Par Diabolik dans le forum Assembleur
    Réponses: 3
    Dernier message: 08/12/2003, 12h15

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