php - 从 PHP INNER JOIN 表创建多维 JSON

标签 php mysql arrays json

我是一个刚刚学习的新人。我的 mySQL 数据库中有 3 个表,第一个“用户”,第二个产品_详细信息和第三个产品_图像。他们使用外键加入。喜欢 : mySql Tables ,我正在使用 php PDO oops 来实现所需的输出。
我想显示带有产品图像的特定用户产品的列表。 所以我得到的对象如下:

[
   {
      "user_id": "1",
      "product_id": "7",
      "product_name": "product title",
      "product_description": "product description",
      "product_img_id": "1",
      "product_img_path": "/products/product1.jpg",
      "product_img_type": "main"
   },
   { ...  },
   { ...  },
   { ...  },
]

我的 Php 对象类功能代码:

function read(){
   $query = "SELECT
               p.user_id, p.product_id, p.product_name, p.product_description, 
           p1.product_img_id, p1.product_img_path, p1.product_img_type
    FROM        user AS p
    LEFT JOIN   product_description AS p1  
    ON       p.product_id = p1.product_id
    ORDER BY    p.user_id DESC, p.product_id DESC";
    $stmt = $this->conn->prepare($query);
    $stmt->execute();
    return $stmt;
}

这是我的创建对象代码:

if ($num > 0) {
   $products_arr = array();
   while ($row = $stmt -> fetch(PDO:: FETCH_ASSOC)) {
      extract($row);
      $product_item = array(
         "user_id" => $user_id,
         "product_id" => $product_id,
         "product_name" => $product_name,
         "product_description" => $product_description,
         "product_img_id" => $product_img_id,
         "product_img_path" => $product_img_path,
         "product_img_type" => $product_img_type
      );
      $products_arr[] = $product_item;
   }
   echo json_encode($products_arr);
}

所以我需要一个像这样的对象:

{
   "user_id" : "1",
      "product" : [
         {
            "user_id": "1",
            "product_id": "1",
            "product_name": "product title ",
            "product_description": "this is product description",
            "product_images": [
               {
                  "product_img_id": "1",
                  "product_img_path": "/products/product1.jpg",
                  "product_img_type": "main"
               },
               {...},
               {...},
               {...},
               {...}
            ]
         },
         {...},
         {...},
         {...}
      ]
}

我想要

{ **user**
   { all **product** of this user {
        all **images** of this product
     }
   }
}

最佳答案

在您的 php 代码中,您可以将以下代码更改为底部的代码

$product_item = array(
     "user_id" => $user_id,
     "product_id" => $product_id,
     "product_name" => $product_name,
     "product_description" => $product_description,
     "product_img_id" => $product_img_id,
     "product_img_path" => $product_img_path,
     "product_img_type" => $product_img_type
  );

以下内容:

$product_image = array(
   "product_img_id"   => $product_img_id,
   "product_img_path" => $product_img_path,
   "product_img_type" => $product_img_type
);

$product = [
    "product_id" => $product_id,
    "product_name" => $product_name,
    "product_description" => $product_description,
    "product_images" => $product_image
];

$product_item = array(
    "user_id" => $user_id,
    "product" => $product
);

关于php - 从 PHP INNER JOIN 表创建多维 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699300/

相关文章:

php - 验证日程安排的日期时间输入

php - 如何获取运行时确定的可调用参数的数量?

php - 在 Windows 上使用 php 和 pear 发送邮件

java - 将项目添加到有序集中的数组的问题

Java-将元素从 ArrayList 移动到数组

php - 如何一次循环遍历两个数组?

php - 链接样式表中缺少 CSS 元素

mysql - 根据重复列返回值

php - 在数字前使用 {%2B) 而不是 (+) 有什么区别?

使用 ORDER BY 时 MySQL 查询变得非常慢