I've already looked at this thread: PHP decode nested JSON and haven't managed to use it to solve my problem.
I am currently grabbing a JSON object from Mongo, and I'm having issues grabbing information from nested objects.
{
"adminLevel" : 200,
"chat" : true,
"clans" : [
BinData(0,"wcXHR577OVBXfy9JwEf5gQAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
],
"experience" : NumberLong(70003),
"kitNew" : {
"converted" : true,
"items" : {
"ak47" : {
"killCount" : 5,
"selected" : false,
"unlocked" : 1
},
"hub-knife" : {
"selected" : false
},
"assault" : {
"selected" : false,
"unlocked" : 1
},
"pistol" : {
"deathWhileSelectedCount" : 3,
"killedBySelectedCount" : 1,
"killWhileSelectedCount" : 1,
"selected" : false,
"unlocked" : 1
},
"m1014" : {
"deathWhileSelectedCount" : 3,
"killedBySelectedCount" : 1,
"killCount" : 17,
"killWhileSelectedCount" : 1,
"killedByCount" : 1,
"selected" : false,
"unlocked" : 1
},
},
},
"points" : NumberLong(87167),
}
My aim here is to print out information about each of the items, my PHP code is as follows
// execute query
// retrieve all documents
$query = array("lastKnownUsername" => "Strubo");
$cursor = $collection->find($query);
$array = json_decode($cursor, true);
$items = $array->kitNew->items;
foreach($items as $item){
echo "<tr>";
echo "<td>" . $item . "</td>";
echo "<td>" . $item->killCount . "</td>";
echo "<td>" . $item->killedByCount . "</td>";
echo "<td>" . $item->selected . "</td>";
echo "<td>" . $item->unlocked . "</td>";
echo "</tr>";
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
I know that there is no problems connecting to the database, or any environment issues, as other displays of grabbed data work. It's just nesting which is the issue here.
Thanks.