-1

So, here's what I use to get my data:

    $app->get('/people', 'getPeople');

    function getPeople(){
        $sql = "SELECT * FROM people";    
        try {
            $db = getConnection();
            $stmt = $db->query($sql);
            $entries = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db = null;
            echo json_encode($entries);
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }

And here's the output of my collection (testsite.local/api/index.php/people) which looks something like this:

    [
        {
            "month":"January",
            "week":"week1",
            "day":"monday",
            "photo":"pic.jpg",
            "name":"Mona Lisa"
        },
        {
            "month":"January",
            "week":"week2",
            "day":"monday",
            "photo":"pic.jpg",
            "name":"Michael Angelo"
        },
        {
            "month":"February",
            "week":"week1",
            "day":"tuesday",
            "photo":"pic.jpg",
            "name":"Da Vinci"
        }
    ]

So my question is, WHERE and WHAT do I edit so that the output becomes like this?? (Is this even possible?)

    [
        {
            "month":[
                {
                    "January":[
                        {
                            "week1":[
                                {
                                    "day":"monday",
                                    "photo":"pic.jpg",
                                    "name":"Mona Lisa"
                                }
                            ],
                            "week2":[
                                {
                                    "day":"monday",
                                    "photo":"pic.jpg",
                                    "name":"Michael Angelo"
                                }
                            ]
                        }
                    ],
                    "February":[
                        {
                            "week1":[
                                {
                                    "day":"tuesday",
                                    "photo":"pic.jpg",
                                    "name":"Da Vinci"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]

I am quite new at this and I really can't figure it out.. Thanks in advance :)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
lemeown
  • 11

2 Answers2

0

I guess this may work

    <?php
         $newArray = array();
         foreach($entries as $key => $entry){

         if($key === 'month'){
             $newArray[$key][$entry][$entries['week']] = array($entries->day,$entries->photo,$entries->name) 
         }
     }

     echo json_encode($newArray);
Anand G
  • 3,130
  • 1
  • 22
  • 28
0

You could always use a parse() function to do any processing to the API response before you send it to the view:

var PeopleCollection = Backbone.Collection.extend({
    url: '/people',
    parse: function(data) {
        // do some processing
        return data;
    }
})
T Nguyen
  • 3,309
  • 2
  • 31
  • 48