JSON, faire une boucle sur les objets

Résolu
lemega Messages postés 87 Date d'inscription mardi 13 janvier 2009 Statut Membre Dernière intervention 31 mai 2023 - 12 déc. 2018 à 16:01
lemega Messages postés 87 Date d'inscription mardi 13 janvier 2009 Statut Membre Dernière intervention 31 mai 2023 - 12 déc. 2018 à 18:35
Bonjour chers tous,

Tout d'abord je tiens à préciser que je suis un debutant en JSON.
Merci pour la compreension.
Voici le contenu de mon JSON
G:\wamp64\www\promolink\traiter-json.php:7:
object(stdClass)[1]
public 'msg' => string '0' (length=1)
public 'compte' =>
object(stdClass)[2]
public 'idAuthentification' => int 1
public 'idRoles' => int 1
public 'nom' => string 'JR' (length=2)
public 'prenom' => string 'Ross' (length=4)
public 'tel1' => string '45193959' (length=8)
public 'tel2' => string '07035464' (length=8)
public 'adressePostale' => string '01 BP 4054 ABJ 01' (length=17)
public 'fonction' => string 'ingenieur informaticien' (length=23)
public 'ville' => string 'Abidjan' (length=7)
public 'lieuHabitation' => string 'yop' (length=3)
public 'pays' => string 'CI' (length=2)
public 'photo' => string '1.jog' (length=5)
public 'nationalite' => string 'ivoirienne' (length=10)
public 'email' => string 'romaric.guindo@gmail.com' (length=24)
public 'dateCreation' => null
public 'dateModification' => null
public 'dateDerniereVisite' => null
public 'visible' => boolean false
public 'Id' => int 5
public 'menu' =>
array (size=4)
0 =>
object(stdClass)[3]
public 'libelle' => string 'TMA' (length=3)
public 'refmenu' => string '4' (length=1)
public 'url' => string 'http' (length=4)
public 'statut' => boolean true
public 'Id' => int 1
1 =>
object(stdClass)[4]
public 'libelle' => string 'Dashboard' (length=9)
public 'refmenu' => string '1' (length=1)
public 'url' => string 'http' (length=4)
public 'statut' => boolean true
public 'Id' => int 4
2 =>
object(stdClass)[5]
public 'libelle' => string 'Informations programme' (length=22)
public 'refmenu' => string '2' (length=1)
public 'url' => string 'http' (length=4)
public 'statut' => boolean true
public 'Id' => int 3
3 =>
object(stdClass)[6]
public 'libelle' => string 'Echange acquéreurs' (length=19)
public 'refmenu' => string '3' (length=1)
public 'url' => string 'http' (length=4)
public 'statut' => boolean true
public 'Id' => int 2


Dans l'objet 'menu' on observe 4 blocs(de 0=> à 3 =>)

Alors je souhaite afficher ces blocs en php en effectuant une boucle soit "foreach" ou "for".

Ainsi donc je souhaiterais avoir une aide de votre part afin de resoudre programme.

Merci d'avance !!

2 réponses

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
12 déc. 2018 à 16:42
Bonjour
Regarde la fonction json_decode()
1
lemega Messages postés 87 Date d'inscription mardi 13 janvier 2009 Statut Membre Dernière intervention 31 mai 2023 1
12 déc. 2018 à 16:59
merci
0
lemega Messages postés 87 Date d'inscription mardi 13 janvier 2009 Statut Membre Dernière intervention 31 mai 2023 1
12 déc. 2018 à 16:56
Merci jordane45 pour votre reonse.

De façon individuelle, j'arrive à les donnees de chaque objet.
<?php
$json = file_get_contents("fichier.json");
$parsed_json = json_decode($json);

$menu_libelle = $parsed_json ->{'menu'}[0]->{'libelle'};

?>

Ce que je souhait faire c'est par exemple afficher les donnees comme sute:

Pour Objet 0 => (tableau N1)
Libelle : TMA
Reference menu : 4
Url : http
Statut : true
Id : 1

Pour Objet 1 => (tableau N2)
Libelle : Dashboard
Reference menu : 1
Url : http
Statut : true
Id : 4

et ainsi de suite.

je veux au fait afficher les blocs a partir d'une boucle
0
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
12 déc. 2018 à 18:21
$parsed_json = json_decode($json,true);// transforme le json en array

$menu = $parsed_json['menu'];

foreach($menu as $M){
   echo "<br>" . $M['Libelle'];
}

0
lemega Messages postés 87 Date d'inscription mardi 13 janvier 2009 Statut Membre Dernière intervention 31 mai 2023 1
Modifié le 12 déc. 2018 à 18:36
Ok... Super... ca marche.. grand merci a vous , jordane45
0
Rejoignez-nous