为了解释我的问题,我有 3 个与饮料制造商相关的信息表。它们类似于以下内容:
t_manufacturers
___________________________________
| ID | name | phone |
| 1 | Coke | 123456 |
| 2 | Pepsi | 654321 |
___________________________________
t_drinks
___________________________
| ID | name |
| 2 | cherry Pepsi |
| 1 | cherry coke |
___________________________
t_brandlogos
__________________________
| ID | imagename |
| 1 | original_coke |
| 2 | original_pepsi |
| 2 | cherry_pepsi |
| 1 | cherry_coke |
__________________________
我想查询这些数据,这样我就可以获得一个 json 文件,其中包含每个制造商及其所有品牌 Logo /图像和可用饮料,与表之间的 ID 相匹配。
如果我只查询可乐信息,我会想象并希望“漂亮”的 json 读起来与此类似:
[
{
“ID”:1,
“name”:”coke”,
“drinks”:
[{“name”:”cherry coke”},
{“name”:”coke”}],
“images”:
[{“imagename”:”original_coke”},
{“imagename”:”cherry_coke”}]
]
我正在使用 PHP 从我的网站运行查询并返回 json。我遇到的问题是,我不知道如何编写 MySQL 查询逻辑以将结果获取到 php 中,以便 php 可以以 json 格式返回数据。
更清楚地说,我可以让它做一些事情,但我似乎无法让 query/php 正确地构造传出的 json,所有数据都不包含重复信息。
如有任何指导,我们将不胜感激。
编辑***
我认为执行此操作的方法是在 PHP 中运行 3 个查询,最终得到一系列饮料制造商及其饮料/图像数组(如我所需的 json 输出所建议的那样)。
为此,我想我会查询饮料制造商的 ID/名称,然后分别查询每个制造商的饮料/ Logo 数组。
php 是否能够做到这一点...它会像 c++ 一样简单。
编辑结束***
新编辑***
所以我确实设法将输出获取到数组数组,但是我现在遇到的问题是输出不是具有饮料/图像数组,例如:
“drinks”:
[{“name”:”cherry coke”},
{“name”:”coke”}],
“images”:
[{“imagename”:”original_coke”},
{“imagename”:”cherry_coke”}]
]
相反,“饮料”和“图像”被替换为“0”和“1”。
到目前为止,我完成此操作的方法是查询所有制造商,然后对于每个制造商,我使用 ID 查询所有饮料。我获取新的饮料数组,然后将其附加到当前制造数组的末尾,对于图像也是如此,然后将当前制造商添加到制造商数组中。我用这个 smie-sudocode 中的想法完成了这些循环:
for_each_manufacturer{
for_each_drink{
array_push($drinkArray, $tempDrinkArray);
}
array_push($currentManufacturerArray, $drinkArray);
for_each_image{
array_push($imageArray, $tempImageArray);
}
array_push($currentManufacturerArray, $imagesArray);
array_push($manufacturerArray, $currentManufacturerArray);
}
我现在要解决的问题(因为我得到了正确的输出格式)是将“0”和“1”分别更改为“饮料”和“图像”。
感谢迄今为止提供帮助的所有人!
结束新编辑***
我只需将旧数据的副本添加到数组中即可解决此问题,但使用不同的 key ,然后将数据放在旧 key 下。
$currentManufacturerArray["images"] = $currentManufacturerArray["1"];
unset($currentManufacturerArray["1"]);
最佳答案
嗨 Charles,欢迎来到 Stack Overflow。
我建议将其分为三个不同的查询,第一个查询填写公司,第二个查询添加饮料,第三个查询最后添加图像。
由于您是 PHP 新手,这里有一些如何构建结果的指导:
// define the response array
$responseArray = [];
// Step 1: fill in the companies, using the company ID as array key ("1" in the demo)
// replace this with your first mysqli_query();
// we will need the empty arrays later on.
$responseArray[1] = [
'ID' => 1,
'name' => 'Coca Cola',
'drinks' => [],
'images' => []
];
// Step 2: push in the drinks into the "drinks" sub-array, using the company ID and 'drinks' as array keys ("1" in the demo)
// this would happen in your second mysqli_query();
$responseArray[1]['drinks'][] = ['name' => 'Cherry Cola'];
$responseArray[1]['drinks'][] = ['name' => 'Coca cola Coke'];
// Step 2: push in the images into the "drinks" sub-array, using the company ID and 'images' as array keys ("1" in the demo)
// this would happen in your third mysqli_query();
$responseArray[1]['images'][] = ['imagename' => 'original_coke'];
$responseArray[1]['images'][] = ['imagename' => 'cherry_coke'];
// output the result as JSON. The array_values(...) call makes sure we won't have the company ID as array key in our result
header('Content-Type: application/json');
print json_encode(array_values($responseArray));
这会给你以下结果:
[
{
"ID": 1,
"name": "Coca Cola",
"drinks": [
{
"name": "Cherry Cola"
},
{
"name": "Coca cola Coke"
}
],
"images": [
{
"imagename": "original_coke"
},
{
"imagename": "cherry_coke"
}
]
}
]
要循环公司表并填写值,您可以使用类似于以下的代码:
// no error handling as this is just a functionality sample ...
$responseArray = [];
$connection = mysqli_connect('HOST', 'USER', 'PASS', 'DATABASE');
// add the companies ...
$query = '
SELECT
ID, name
FROM
t_manufacturers';
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$responseArray[$row['ID']] = [
'id' => $row['ID'],
'company' => $row['name'],
'drinks' => [],
'images' => []
];
}
这将为您提供包含所有公司的结果。如果您只想输出一家公司,请使用 WHERE
子句作为三个查询的一部分。在这个简单的演示中,您还可以使用以下方法从响应数组中选择一家公司:
print json_encode($responseArray[1]); // would give you the Coca section
请告诉我这是否对您有帮助。
关于php - 查询多个表 MySQL 不同大小的 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56051796/