要解释我的问题,我有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文件,其中包含所有品牌徽标/图像和饮料,与表之间的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格式返回数据。
更清楚,我可以做些事情,但是我似乎无法获得查询/php来正确构建外发JSON,并使用包含重复信息的所有数据。
任何指导都将不胜感激。
编辑***
我认为,这样做的方法将在PHP中运行3个查询,最终会带有一系列带有饮料/图像阵列的饮料制造商(正如我所需的JSON输出所建议的(。
为了做到这一点,我想我会查询饮料制造商的ID/名称,然后为每个制造商单独查询其饮料/徽标阵列。
是PHP能够做到这一点的……在C 中进行的操作很容易。
结束编辑***
新的Edite ***
所以我确实设法使输出到达数组,但是我现在遇到的问题是,而不是输出一系列饮料/图像,例如:
“drinks”:
[{“name”:”cherry coke”},
{“name”:”coke”}],
“images”:
[{“imagename”:”original_coke”},
{“imagename”:”cherry_coke”}]
]
相反,"饮料"one_answers"图像"被" 0"one_answers" 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"one_answers" 1"更改为"饮料"one_answers"图像"。
感谢所有一直在提供帮助的人!
结束新的编辑***
我能够通过仅将旧数据的副本添加到数组中来解决此问题,但使用不同的键,然后将数据放在旧密钥下。
$currentManufacturerArray["images"] = $currentManufacturerArray["1"];
unset($currentManufacturerArray["1"]);
嗨查尔斯(Charles(,欢迎来到堆栈溢出。
我建议将其分为三个不同的查询,首先要填写公司,其次要添加饮料,三分之一以最终添加图像。
您是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
让我知道这是否对您有帮助。