Jump to content

Recommended Posts

Dear All,

As I would like to create json as below format 

{
    "data": [
        {
            "title": "After getting a COVID-19 Vaccine",
            "content": [
                {
                    "list": "You may have some side effects, which are normal signs that your body is building protection."
                },
                {
                    "list": "Following symptoms can take place, and it usually resolves within 2 to 3 days."
                },
                {
                    "list": "Common Side Effects pain, redness, swelling, warmth, nausea, muscle pain, tiredness, headache"
                }
            ]
        },
        {
            "title": "After getting a COVID-19 Vaccine",
            "content": [
                {
                    "list": "You may have some side effects, which are normal signs that your body is building protection."
                },
                {
                    "list": "Following symptoms can take place, and it usually resolves within 2 to 3 days."
                },
                {
                    "list": "Common Side Effects pain, redness, swelling, warmth, nausea, muscle pain, tiredness, headache"
                }
            ]
        }
    ]
}

The information is from  mysql select command:

SELECT (tb_helpinfo.title_eng) as title,(tb_helpdetail.content_eng) as content FROM tb_helpinfo INNER JOIN tb_helpdetail ON tb_helpinfo.id = tb_helpdetail.helpinfo_id

 


How could I make json like that from mysql information?

mysql.PNG

Link to comment
https://forums.phpfreaks.com/topic/312608-php-create-json-from-inner-join-table/
Share on other sites

Depends the structure you want for your array. Using your existing query...

$res = $pdo->query("SELECT i.title_eng as title
                         , d.content_eng as content 
                    FROM tb_helpinfo i
                         INNER JOIN tb_helpdetail d ON i.id = d.helpinfo_id
                    ORDER BY title
                    ");
## METHOD 1
    $data = [];
    foreach ($res as $r)  {
        if (!isset($data[$r['title']])) {
            $data[$r['title']] = [];
        }
        $data[$r['title']][] = $r['content'];
    }
    echo '<pre> Method 1 ', print_r($data, 1), '</pre>';
    echo json_encode($data);
    
## METHOD 2
    $data = $res->fetchAll();
    echo '<pre> Method 2 ', print_r($data, 1), '</pre>';
    echo json_encode($data);
    

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.