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 :

[Json et php] construction tableau..


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut [Json et php] construction tableau..
    Bonjour,

    je voudrait avoir un peu d'aide..

    je doit afficher un tableau d'une structure JSON

    mais je n'arrive pas a construire mon tableau

    voici le json :

    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
    array (size=3)
      0 => 
        array (size=3)
          0 => 
            array (size=2)
              'idauteur' => 
                array (size=3)
                  'idauteur' => int 1
                  'nomauteur' => string 'Dupond' (length=6)
                  'prenomauteur' => string 'Pierre' (length=6)
              'idlivre' => 
                array (size=3)
                  'idlivre' => int 1
                  'titre' => string 'Php avances' (length=11)
                  'idlangue' => 
                    array (size=2)
                      'idlangue' => int 1
                      'libelle' => string 'Francais' (length=8)
          1 => 
            array (size=2)
              'idauteur' => 
                array (size=3)
                  'idauteur' => int 2
                  'nomauteur' => string 'SaintPierre' (length=11)
                  'prenomauteur' => string 'Lisa' (length=4)
              'idlivre' => 
                array (size=3)
                  'idlivre' => int 1
                  'titre' => string 'Php avances' (length=11)
                  'idlangue' => 
                    array (size=2)
                      'idlangue' => int 1
                      'libelle' => string 'Francais' (length=8)
          2 => 
            array (size=2)
              'idauteur' => 
                array (size=3)
                  'idauteur' => int 2
                  'nomauteur' => string 'SaintPierre' (length=11)
                  'prenomauteur' => string 'Lisa' (length=4)
              'idlivre' => 
                array (size=3)
                  'idlivre' => int 2
                  'titre' => string 'Java avances' (length=12)
                  'idlangue' => 
                    array (size=2)
                      'idlangue' => int 1
                      'libelle' => string 'Francais' (length=8)
    je voudrait construire mon tableau et l'envoyer a ma vue de template twig..

    voici mon code qui plante:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for($i=0;$i<count($array[0]);$i++)
            {
                $tabAuteur['tabAuteur'][$array[0][$i]['idauteur']] =
                    array('idauteur' => $array[0][$i]['idauteur'],
                        'nomauteur' => $array[0][$i]['nomauteur'],
                    );
            }
    merci d'avance pour votre aide..

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Ce n'est pas un Json que tu nous montres c'est un tableau PHP.
    Du coup je ne comprends pas, ce que tu nous montres c'est ce que tu veux obtenir ou c'est ta source ?

  3. #3
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut C'est bien un tableau php..
    Merci pour ta réaction..

    Je me suis trompé

    C'est bien un tableau php et non un json..

    J'ai affiché le tableau avec un vardump

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    et donc qu'est ce que tu veux obtenir du coup ?

  5. #5
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut Stocker les données
    Je voudrais stocker mes objet dans mon tableau afin de les passer à la vue..

  6. #6
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    De quels objets parles-tu ?

  7. #7
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut Je parle de..
    Je parle de l'objet retourner par la fonction

    Plus précisément de l'objet livre..

    J'ai un objet livre avec des auteurs..

  8. #8
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Il faudrait que tu décrives mieux ce que tu veux parce que là ...
    Tu nous montres un tableau PHP et c'est tout et maintenant tu nous parles d'une fonction et d'objets livre et auteur que tu voudrais mettre dans ce tableau qui contient lui même déjà des livres et des auteurs ... bref moi je ne comprends rien.

  9. #9
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut je ne comprend rien..
    merci pour ta réaction..

    mais je ne suis peut être pas clair..

    je m'en excuse.

    voici le json dont je parle et je doit construire un tableau à partir de ce json

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [[{"idauteur":{"idauteur":1,"nomauteur":"Dupond","prenomauteur":"Pierre"},"idlivre":{"idlivre":1,"titre":"Php avances","idlangue":{"idlangue":1,"libelle":"Francais"}}},{"idauteur":{"idauteur":2,"nomauteur":"SaintPierre","prenomauteur":"Lisa"},"idlivre":{"idlivre":1,"titre":"Php avances","idlangue":{"idlangue":1,"libelle":"Francais"}}},{"idauteur":{"idauteur":2,"nomauteur":"SaintPierre","prenomauteur":"Lisa"},"idlivre":{"idlivre":2,"titre":"Java avances","idlangue":{"idlangue":1,"libelle":"Francais"}}}]]
    je ne voit pas comment expliquer autrement..

    merci d'avance..

  10. #10
    Expert éminent Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 905
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 905
    Points : 6 693
    Points
    6 693
    Par défaut
    Peux-tu nous montrer le code de ta classe livre?

  11. #11
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut code des entiters?
    Merci pour ta réaction..

    tu veux bien être plus précis..

    je pense que tu veux parler de mes entités..?

    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
    <?php
     
    namespace Symfony\RestBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Livre
     *
     * @ORM\Table(name="Livre", indexes={@ORM\Index(name="FK_Livre_idLangue", columns={"idLangue"})})
     * @ORM\Entity(repositoryClass="Symfony\RestBundle\Repository\LivreRepository")
     */
    class Livre
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="idLivre", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idlivre;
     
        /**
         * @var string
         *
         * @ORM\Column(name="titre", type="string", length=255, nullable=true)
         */
        private $titre;
     
        /**
         * @var \Languelivre
         *
         * @ORM\ManyToOne(targetEntity="Languelivre")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="idLangue", referencedColumnName="idLangue")
         * })
         */
        private $idlangue;
     
     
     
        /**
         * Get idlivre
         *
         * @return integer
         */
        public function getIdlivre()
        {
            return $this->idlivre;
        }
     
        /**
         * Set titre
         *
         * @param string $titre
         *
         * @return Livre
         */
        public function setTitre($titre)
        {
            $this->titre = $titre;
     
            return $this;
        }
     
        /**
         * Get titre
         *
         * @return string
         */
        public function getTitre()
        {
            return $this->titre;
        }
     
        /**
         * Set idlangue
         *
         * @param \Symfony\RestBundle\Entity\Languelivre $idlangue
         *
         * @return Livre
         */
        public function setIdlangue(\Symfony\RestBundle\Entity\Languelivre $idlangue = null)
        {
            $this->idlangue = $idlangue;
     
            return $this;
        }
     
        /**
         * Get idlangue
         *
         * @return \Symfony\RestBundle\Entity\Languelivre
         */
        public function getIdlangue()
        {
            return $this->idlangue;
        }
    }
    ma classe est relier à une table avec une relation 1,N pour avoir plusieurs auteurs pour un livre:

    voici ma classe ecritPar:

    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
    <?php
     
    namespace Symfony\RestBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Ecritpar
     *
     * @ORM\Table(name="EcritPar")
     * @ORM\Entity(repositoryClass="Symfony\RestBundle\Repository\EcritParRepository")
     */
    class Ecritpar
    {
        /**
         * @var \Auteur
         *
         * @ORM\ManyToOne(targetEntity="Auteur")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="idAuteur", referencedColumnName="idAuteur")})
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="NONE")
         *
         */
        private $idauteur;
     
        /**
         * @var \Livre
         *
         * @ORM\ManyToOne(targetEntity="Livre")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="idLivre", referencedColumnName="idLivre")})
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="NONE")
         *
         */
        private $idlivre;
     
     
     
        /**
         * Set idauteur
         *
         * @param integer $idauteur
         *
         * @return Ecritpar
         */
        public function setIdauteur($idauteur)
        {
            $this->idauteur = $idauteur;
     
            return $this;
        }
     
        /**
         * Get idauteur
         *
         * @return integer
         */
        public function getIdauteur()
        {
            return $this->idauteur;
        }
     
        /**
         * Set idlivre
         *
         * @param integer $idlivre
         *
         * @return Ecritpar
         */
        public function setIdlivre($idlivre)
        {
            $this->idlivre = $idlivre;
     
            return $this;
        }
     
        /**
         * Get idlivre
         *
         * @return integer
         */
        public function getIdlivre()
        {
            return $this->idlivre;
        }
    }
    et voici ma classe auteur:

    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
    <?php
     
    namespace Symfony\RestBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Auteur
     *
     * @ORM\Table(name="Auteur")
     * @ORM\Entity
     */
    class Auteur
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="idAuteur", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idauteur;
     
        /**
         * @var string
         *
         * @ORM\Column(name="nomAuteur", type="string", length=255, nullable=true)
         */
        private $nomauteur;
     
        /**
         * @var string
         *
         * @ORM\Column(name="prenomAuteur", type="string", length=255, nullable=true)
         */
        private $prenomauteur;
     
     
     
        /**
         * Get idauteur
         *
         * @return integer
         */
        public function getIdauteur()
        {
            return $this->idauteur;
        }
     
        /**
         * Set nomauteur
         *
         * @param string $nomauteur
         *
         * @return Auteur
         */
        public function setNomauteur($nomauteur)
        {
            $this->nomauteur = $nomauteur;
     
            return $this;
        }
     
        /**
         * Get nomauteur
         *
         * @return string
         */
        public function getNomauteur()
        {
            return $this->nomauteur;
        }
     
        /**
         * Set prenomauteur
         *
         * @param string $prenomauteur
         *
         * @return Auteur
         */
        public function setPrenomauteur($prenomauteur)
        {
            $this->prenomauteur = $prenomauteur;
     
            return $this;
        }
     
        /**
         * Get prenomauteur
         *
         * @return string
         */
        public function getPrenomauteur()
        {
            return $this->prenomauteur;
        }
    }
    merci d'avance.

  12. #12
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    je doit construire un tableau à partir de ce json
    Utilise json_decode() puisque tu n'as pas d'autre contrainte.

  13. #13
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut Json_decode..
    J'utilise déjà le json_decode..

    Ce que j'ai besoin c'est d'envoyer à la vue les données afin de les traiter dans mon template avec twig..

  14. #14
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Dans ton message de départ tu disais que tu n'arrivais pas à construire le tableau, ce n'est plus le cas ?

    Si ton problème concerne purement symfony, pose plutôt ta question precise sur le forum symfony.

  15. #15
    Membre régulier
    Homme Profil pro
    Symfony - CMS Wordpress - Zend
    Inscrit en
    Septembre 2011
    Messages
    306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Symfony - CMS Wordpress - Zend
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 306
    Points : 101
    Points
    101
    Par défaut J'ai besoin de construire un tableau PHP
    désolé..

    je pensait pas pouvoir decoder le json dans mon template twig..

    tu peux me dire si le json que j'ai fourni est correct?

Discussions similaires

  1. [Tableaux] Fonction PHP et tableau à 2 dimensions
    Par ponteprimo dans le forum Langage
    Réponses: 3
    Dernier message: 06/09/2006, 14h21
  2. Réponses: 3
    Dernier message: 04/08/2006, 19h24
  3. [Tableaux] PHP Construction et Tri de tableau PHP
    Par sirbaldur dans le forum Langage
    Réponses: 4
    Dernier message: 29/06/2006, 15h12
  4. Réponses: 8
    Dernier message: 30/04/2006, 15h42
  5. [PHP-JS] Passer un tableau php en tableau JS
    Par dark_vidor dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 22/12/2005, 11h36

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