Creating json in php

It seems that I would hop through hoops to move data from MySQL to json via PHP json_encode - take care of the encoding, pull the related data from the database and make sure you have an array inside the array inside the array (snapshot below) ... is is it a json framework or something where you feed its sql using join tables and it does all the redundant / confusing work for you?

        function people_list() {
        $data['jdata'] =
            array(
                'groups' => 
                    array(
                        array(
                            'name' => 'DeveloperList',
                            'people' => array(
                                            array('name' => 'sam sneed'),
                                            array('name' => 'sue summer')
                                        )
                        ),
                        array(
                            'name' => 'PMList',
                            'people' => array(
                                            array('name' => 'tim pm'),
                                            array('name' => 'sara pm')
                                        )
                        ),
                        array(
                            'name' => 'ClientList',
                            'people' => array(
                                            array('name' => 'Mr Smith'),
                                            array('name' => 'Ms Jones')
                                        )
                        )
                    )
            );

        $this->load->view('json', $data);            
    }
+3
source share
3 answers

If you used ORM like Doctrine , you could return your data as a more structured array that would be saved when json_encode () is called.

.

+4

PDO,

$pdo_stmt = PDO::prepare($your_sql);
$pdo_stmt->execute();
$json_obj = json_encode($pdo_stmt->fetchAll(PDO::FETCH_ASSOC));

, JSON .

+3

If I do not understand your question / problem correctly, it sounds as if you are going to fix it wrong. If you use the proper degree of abstraction, then you can easily check the data structure coming into the database, and then it is reasonable to expect the same structure to return again.

This is one of those principles of "garbage, garbage" - first of all, save time on garbage.

0
source

Source: https://habr.com/ru/post/1717160/


All Articles