MySQL results to JSON with PHP
===

I've seen lots of similar question on here, but I haven't been able to get my desired output. Could someone please help me out? How can generate the following JSON structure? My query is fine, I just can't figure out how to loop through and get this. Thanks

    {
        "players": [
            {
                "id": 271,
                "fname": "Carlos",
                "lname": "Beltran",
                "position": "OF",
                "stats": [
                    {
                        "year": "2010",
                        "hr": 32,
                        "rbi": 99,
                        "team": "NYM"
                    },
                    {
                        "year": "2011",
                        "hr": 35,
                        "rbi": 100,
                        "team": "STL"
                    }, 
                    {
                    ............
                    }
                ]
            },
            {
              ........
            }
        ]
    }