php - 将 PHP/MySQL 数组编码为 JSON 时出现问题

标签 php mysql sql json

我在 MySQL 中有以下表:

recipe: id_recipe (PKey), name_recipe, description
url_pic: id_img, url_img, id_recipe (FKey)

使用 PHP,我尝试获取包含每个 recipe 及其图片 url(数组)的 JSON。我的问题是食谱 2 的图片数组包含两个食谱的图片!我是初学者,找不到问题出在哪里。我在下面包含了我想要的 JSON、我的 PHP 代码和我实际获得的 JSON:

所需的 JSON:

[
    {
        "recette": {
            "id_recipe": "1",
            "name_recipe": "..",
            "description":"..."
            },
        "pictures": [
            {
                "id_url": "1",
                "url": "picture 1 of recipe 1"
            },
            {
                "id_url": "2",
                "url": "picture 2 of recipe 1"
            }      
        ]
    },
    {
        "recette": {
            "id_recipe": "2",
            "name_recipe": "....",
            "description": "...."
        },
        "pictures": [
            {
                "id_url": "3",
                "url": "picture 1 of recipe 2"
            },
            {
                "id_url": "4",
                "url": "picture 2 of recipe 2"
            }

        ]
    }
]

PHP:

$sql = "SELECT id_recipe,name_recipe,description 
        FROM recipe";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data['recipe'] = $row;   
        $sql2= "SELECT id_url,url 
                FROM url_img as u
                WHERE u.id_recipe = '$row[id_recipe]'";         

        $result2 = $conn->query($sql2);   
        if($result2->num_rows > 0){
            while($row2 = $result2->fetch_array(MYSQL_ASSOC)) {
                $array[] = $row2;                  
            }
            $data['pictures'] = $array; 
        }

        $all[] = $data; 
    }

    header('Content-Type: application/json');
    echo json_encode($all,JSON_UNESCAPED_UNICODE);
}

实际 JSON:

    [
        {
            "recette": {
                "id_recipe": "1",
                "name_recipe": "..",
                "description":"..."
                },
            "pictures": [
                {
                    "id_url": "1",
                    "url": "picture 1 of recipe 1"
                },
                {
                    "id_url": "2",
                    "url": "picture 2 of recipe 1"
                }      
            ]
        },
        {
            "recette": {
                "id_recipe": "2",
                "name_recipe": "....",
                "description": "...."
            },
            "pictures": [
                {
                    "id_url": "1",
                    "url": "picture 1 of recipe 1"
                },
                {
                    "id_url": "2",
                    "url": "picture 2 of recipe 1"
                },
                 {
                    "id_url": "3",
                    "url": "picture 1 of recipe 2"
                },
                {
                    "id_url": "4",
                    "url": "picture 2 of recipe 2"
                }

            ]
        }
    ]

最佳答案

在每次迭代中重置数组

$result2 = $conn->query($sql2);
$array = []; // Add this line
if($result2->num_rows > 0){

关于php - 将 PHP/MySQL 数组编码为 JSON 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572366/

相关文章:

javascript - 在模式框/弹出窗口中查看值 - HTML、JavaScript、PHP 和 JavaScript

php - 从 iOS 到 php 服务器的 POST 数据始终为 NULL - iOS 10、Swift 3、php

javascript - 如何使用 PHP + JS 重新加载页面并显示查询参数?

php - 有没有办法在 PHP 匿名函数中不捕获 $this?

java - JPA 两个事务更新相同的行

mysql - 我可以在 SQL 中组合 2 个级别的计数吗?

sql - postgresql -- Curval 不起作用,使用 PHP PDO

php - 无法打开 phpmyadmin,require_once(./libraries/common.inc.php) : failed to open stream: No such file or directory

MySQL 插入错误代码 1136

mysql - 使用 union 时如何组合具有不同列数的 SELECT 语句?