2014-04-10 20:34
浏览 56


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" : [
    "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
} 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.


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumi0737
    doumi0737 2014-04-10 23:12

    MongoDB does not store the data as JSON.

    The document you pasted seems to be copy&paste from the shell. This is not JSON format either. This format is called Extended JSON - and is how the MongoDB shell represents the data in javascript. Most examples on the MongoDB website also use this format as it is simple and easy. So rather then have dozens of different output format (for each language driver), the MongoDB official documentations use the shell for demonstrating functionality and output.

    The actual underlaying format is called Binary JSON (BSON). You will never see this format and you will never interact with it.

    When you interact with MongoDB in PHP all you have to know is that you save a PHP array. The data returned from MongoDB is also a PHP array. The underlaying disk format is not relevant.

    You never have to call json_encode() or json_decode().

    The $collection->find($query) method returns a object called MongoCursor. You should iterate over this object to get the results, which will be a PHP array.

    foreach($collection->find($query) as $result) {

    This code example will var_dump() one result at a time. This result is called a "MongoDB Document" and is similar to "MySQL row". Just like with MySQL, you don't have to know what the underlaying protocol is, or what the underlaying disk format is - that has no affect on you.

    I strongly suggest you read the MongoDB PHP Driver tutorial:

    This should explain the concept a littlebit better, along with how the driver works :)

    点赞 评论