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 :

Construire un array de array


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    70
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 70
    Points : 53
    Points
    53
    Par défaut Construire un array de array
    Bonjour,

    Je cherche à construire un tableau de ce type :
    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
     
    array(12) {
      [0] => array(2) {
          ["id"] => float(31)
          ["name"] => string(6) "France"
        }
      }
      [1] => array(2) {
          ["id"] => float(32)
          ["name"] => string(7) "Germany"
      }
      [2] => array(2) {
          ["id"] => float(36)
          ["name"] => string(7) "Holland"
        }
      }
    }
    La difficulté c’est que je le construis lors d’une itération :

    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
     
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    			$i = 0;
    			foreach ($worksheet->getRowIterator() as $row) {
    				$cellIterator = $row->getCellIterator();
    				$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
    				$j = 0;
    				foreach ($cellIterator as $cell) { 
    					if ($j < 2) {
     
     
     
    					//la construction du tableau 
    						$data[$i][] = array( $name[$j] => $cell->getCalculatedValue() );
     
     
     
    					}
    					$j++;
    				}
    				$i++; 
    			}
    		}
    Le résultat est le suivant

    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
     
    array(12) {
      [0] => array(2) {
        [0] => array(1) {
          ["id"] => float(31)
        }
        [1] => array(1) {
          ["name"] => string(6) "France"
        }
      }
      [1] => array(2) {
        [0] => array(1) {
          ["id"] => float(32)
        }
        [1] => array(1) {
          ["name"] => string(7) "Germany"
        }
      }
      [2] => array(2) {
        [0] => array(1) {
          ["id"] => float(36)
        }
        [1] => array(1) {
          ["name"] => string(7) "Holland"
        }
      }
    Ça semble tout bête mais je n’arrive pas à sortir de ce problème.

    Merci d’avance

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Août 2007
    Messages
    156
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Août 2007
    Messages : 156
    Points : 192
    Points
    192
    Par défaut
    Lors de la création de ton tableau tu as un tableau en trop

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $data[$i][] = array{ ... };
    signifie, à l'index $i du tableau $data mettre un tableau qui contiendra array{...} d'où l'imbrication de 3 tableaux.

    Pour résoudre le problème il te faut juste enlever le array{} et mettre directement tes valeurs dans le deuxième tableau:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $data[$i][$name[$j] = $cell->getCalculatedValue() ;
    Si je ne me trompe pas ça devrait marcher.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    70
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2009
    Messages : 70
    Points : 53
    Points
    53
    Par défaut
    Redoutablement efficace !
    merci Billy14!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. cell array --> double array ?
    Par Truth dans le forum MATLAB
    Réponses: 2
    Dernier message: 06/04/2008, 14h24
  2. Array of array en dynamique
    Par Archimède dans le forum Langage
    Réponses: 16
    Dernier message: 03/11/2007, 17h01
  3. [Tableaux] array => fichier => array ?
    Par adimou dans le forum Langage
    Réponses: 2
    Dernier message: 26/08/2007, 21h22
  4. Déclaration de type array of array of variant
    Par Drooxy dans le forum Delphi
    Réponses: 4
    Dernier message: 14/12/2006, 19h25
  5. Comment faire le tri d'un array of array ?
    Par Tchouffy dans le forum Langage
    Réponses: 1
    Dernier message: 16/06/2005, 20h55

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