2

Here is my JSON data :

{
 "Liste_des_produits1": [{
 "Added_Time": "28-Sep-2009 16:35:03",
 "prod_ingredient": "sgsdgds",
 "prod_danger": ["sans danger pour xyz"],
 "prod_odeur": ["Orange"],
 "prod_nom": "ceciestunproduit",
 "prod_certification": ["HE • Haute Efficité", "Certifier Ecologo", "Contenant recyclable"],
 "prod_photo": "",
 "prod_categorie": ["Corporel"],
 "prod_desc": "gdsg",
 "prod_format": ["10 kg", "20 kg"]
 }, {
 "Added_Time": "28-Sep-2009 16:34:52",
 "prod_ingredient": "dsgdsgdsf",
 "prod_danger": ["Sans danger pour le fausse sceptiques"],
 "prod_odeur": ["Agrumes", "Canneberge"],
 "prod_nom": "jsute un test",
 "prod_certification": ["100% Éco-technologie", "Certifier Ecologo", "Contenant recyclable"],
 "prod_photo": "",
 "prod_categorie": ["Corporel"],
 "prod_desc": "gsdgsdgsdg",
 "prod_format": ["1 Litre", "10 kg"]
 }]
}

In PHP, what is the way to access different values of data?

Like: prod_ingredient or prod_danger.

I have tried:

$prod = $result->{'Liste_des_produits1'};
print_r($prod[0]); // error

and

$result = json_decode($json);
print_r($result['Liste_des_produits1'].prod_ingredient[1]); // error
Mickael Lherminez
6951 gold badge11 silver badges30 bronze badges
asked Sep 30, 2009 at 10:24
1
  • Use var_dump, var_export or print_r to see how the return value looks like. Commented Sep 30, 2009 at 10:35

2 Answers 2

8

Use json_decode to convert the data to an associative array.

$data = json_decode($jsonString, true);
// extend this concept to access other values
$prod_ingredient = $prod['Liste_des_produits1'][0]['prod_ingredient'];
answered Sep 30, 2009 at 10:26
Sign up to request clarification or add additional context in comments.

4 Comments

this code : $json = file_get_contents('produits-lemieux.com/json.txt'); $prod = json_decode($json); $prod_ingredient = $prod['Liste_des_produits1'][0]['prod_ingredient']; print_r($prod_ingredient); result in this error = Fatal error: Cannot use object of type stdClass as array in /home/studiot/public_html/test3.php on line 12
My apologies marc-andre mendard, I should have checked the documentation before answering. I have revised my answer slightly.
just adding TRUE make the whole thng different... can you explain more, but it work, your rock !
Passing true to json_encode() decodes JSON objects as associative arrays rather than PHP objects. I was under the impression this was the default behaviour, and it is more intuitive IMO. In any case, the manual page I linked to explains everything in detail.
0

Use json_decode

Then you can access the data as a regular array.

answered Sep 30, 2009 at 10:26

1 Comment

@strager: The OP has been edited since brainfck posted this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.