icu222much Posted June 10, 2011 Share Posted June 10, 2011 I am trying to extract the list of people in my JSON file, but I keep on receiving an error. I am unsure on how to extract the different people as the names of the people are placed in an array. <?php $jsonFile = file_get_contents('list.json'); $jsonArray = json_decode($jsonFile, true); foreach($jsonArray -> people as $p) { echo $p; } ?> [ { "place": "paris", "people": [ "will", "jake", "melissa" ] }, { "place": "saigon", "people": [ "willis", "john" ] } ] Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/ Share on other sites More sharing options...
Ollifi Posted June 10, 2011 Share Posted June 10, 2011 What error does it give? For which line? Have you tried print_r function ? Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228021 Share on other sites More sharing options...
icu222much Posted June 10, 2011 Author Share Posted June 10, 2011 I have used the var_dump function and I see all of my data from the JSON file being printed. PHP gives me the following errors: Notice: Trying to get property of non-object in C:\xampp\htdocs\jonTest\test.php on line 6 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\jonTest\test.php on line 6 The problem is in the foreach loop. I'm not sure how to tell it to print out each person in the people array. Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228027 Share on other sites More sharing options...
Ollifi Posted June 10, 2011 Share Posted June 10, 2011 I don´t know what is your array like but try with a for loop. <?php $jsonFile = file_get_contents('list.json'); $jsonArray = json_decode($jsonFile, true); for($i=0;$i<count($jsonArray);$i++){ print $jsonArray[$i]; } ?> If it does not work please execute following code and post results here <?php $jsonFile = file_get_contents('list.json'); $jsonArray = json_decode($jsonFile, true); print_r($jsonArray); ?> Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228032 Share on other sites More sharing options...
icu222much Posted June 10, 2011 Author Share Posted June 10, 2011 Ty Ollifi. Just using a plain ordinary for loop worked beautifully Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228035 Share on other sites More sharing options...
Ollifi Posted June 10, 2011 Share Posted June 10, 2011 Ok, good if it helped =) If you don´t need more help at this time,please mark this topic ´solved´. Link to comment https://forums.phpfreaks.com/topic/238998-json-parsing-noob/#findComment-1228040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.