将两个表的结果合并到JSON中

时间:2019-04-14 23:30:38

标签: php mysql json

I asked a similar question before,但没有在问题中包含列book_no,并且由于我想要的解决方案必须包括此列才能为我提供正确的JSON结构,因此我得到的解决方案没有因此工作。因此,我将使用适当的信息重新发布。

我有两个表wordsparagraphwords表如下:

+----+---------+--------------+---------+---------+
| id | book_no | paragraph_no | word_no |   word  |
+----+---------+--------------+---------+---------+
|  1 |    1    |       1      |    1    |  hello  |
+----+---------+--------------+---------+---------+
|  2 |    1    |       1      |    2    |  how    |
+----+---------+--------------+---------+---------+
|  3 |    1    |       1      |    3    |  are    |
+----+---------+--------------+---------+---------+
|  4 |    1    |       1      |    4    |  you    |
+----+---------+--------------+---------+---------+

paragraph表如下:

+----+---------+--------------+-------------------+
| id | book_no | paragraph_no |     paragraph     |
+----+---------+--------------+-------------------+
|  1 |    1    |       1      | hello how are you |
+----+---------+--------------+-------------------+

我希望words表中WHERE book_no的所有列都是1,而段落表中具有相同WHERE子句的段落列都在一个JSON结果中。像这样:

{
    "1": [ <-- this is paragraph_no

        "words": [

            {
                "id": "1",
                "word_no": "1",
                "paragraph_no": "1",
                "word": "hello"
            },
            {
                "id": "2",
                "word_no": "2",
                "paragraph_no": "1",
                "word": "how"
            },

            // and so on...

        ],

        "paragraph": [

            {
                "paragraph": "hello how are you"
            }

        ]

    ]
}

请原谅我的模型,但我需要类似的东西。我目前仅能得到这些单词的PHP代码是:

$result = $conn->query("SELECT * FROM words WHERE book_no = 1");

$data = array();

while ($row = $result->fetch_assoc()) $data[$row['paragraph_no']][] = $row; // paragraph_no from 'words' table

$API_RESULT = json_encode($data, JSON_UNESCAPED_UNICODE);

echo $API_RESULT;

仅输出以下单词:

{
    "1": [ <-- this is paragraph_no

        {
            "id": "1",
            "word_no": "1",
            "paragraph_no": "1",
            "word": "hello"
        },
        {
            "id": "2",
            "word_no": "2",
            "paragraph_no": "1",
            "word": "how"
        },

        // and so on...

    ]
}

如何获取所需的JSON输出?

1 个答案:

答案 0 :(得分:2)

此代码是否产生您需要的输出?

$result_w = $conn->query("SELECT * FROM words;");
$results_w = $result_w->fetch_all(MYSQLI_ASSOC);

$words_per_paragraph = [];
foreach($results_w as $key => $row) {
    $words_per_paragraph[$row['paragraph_no']][] = $row;
}

$result_p = $conn->query("SELECT * FROM paragraph;");
$results_p = $result_p->fetch_all(MYSQLI_ASSOC);

$data = [];
foreach($results_p as $key => $row) {
    $p_no = $row['paragraph_no'];
    $words = [];
    if(array_key_exists($p_no, $words_per_paragraph)) {
        $words = $words_per_paragraph[$p_no];
    }
    $data[$p_no] = [
        'words' => $words,
        'paragraph' => $row
    ];
}

$data的内容(出于测试目的,我没有在第2段中添加任何文字):

{
   "1":{
      "words":{
         "id":"4",
         "book_no":"1",
         "paragraph_no":"1",
         "word_no":"4",
         "word":"you"
      },
      "paragraph":{
         "id":"1",
         "book_no":"1",
         "paragraph_no":"1",
         "paragraph":"hello how are you"
      }
   },
   "2":{
      "words":[

      ],
      "paragraph":{
         "id":"3",
         "book_no":"1",
         "paragraph_no":"2",
         "paragraph":"I'm fine and you?"
      }
   }
}

也许您可以更改数据库表结构以在一条语句中获得所有内容。