多维数组正在覆盖而不是追加



我有一个Wordpress网站Woocommerce订阅。我想列出所有产品,并在每个产品中列出每个订阅(如果有的话)和订阅状态(活动,取消,保持等)。我已经设法把所有的产品列在一个数组中,但是我在组织方面有困难。下面是一个示例代码片段:

[
{
"bod_id": "3557",
"bod_navn": "CS 01",
"order_id": "4153",
"sub_id": "4154",
"sub_status": "wc-active"
},
{
"bod_id": "3557",
"bod_navn": "CS 01",
"order_id": "4570",
"sub_id": null,
"sub_status": null
},
{
"bod_id": "3557",
"bod_navn": "CS 01",
"order_id": "4631",
"sub_id": null,
"sub_status": null
}
]

完整的结果页可以在这里找到:https://inboxlager.no/bodoversikt/index4.php

"bod_id"是产品ID,我希望每个数组项有1个产品。之后是"bod_navn"这是产品的名称。在示例"order_id"是列出的,但除了作为产品和产品内每个订阅之间的链接之外,其他任何地方都不需要它。

使用上面的例子,我希望结果或多或少像这样(附加子):(null的结果也不是真正想要的,我只是添加它们来显示追加,并使用与以前相同的示例)

[
{
"bod_id": "3557",
"bod_navn": "CS 01",
"subs": {
"sub_id": "4154" {
"sub_status": "wc-active"
},
"sub_id": "null" {
sub_status": "null"
},
"sub_id": "null" {
sub_status": "null"
}
}
]

在尝试解决这个问题之后,我更接近了一点,但仍然不够好。结果的一个片段:

{
"3557": {
"bod_id": "3557",
"bod_navn": "CS 01",
"subs": {
"sub_id": {
"sub_status": null
}
}
}
}

完整的结果页可以在这里找到:https://inboxlager.no/bodoversikt/index5.php

如你所见,[]不见了。但更重要的是;sub_id不断被覆盖,只显示最后一行。我有两个理论来解决这个问题,但我不知道如何正确地执行它们。

  1. 运行带有sub的单独数组,然后以某种方式合并。
  2. 在while循环中运行特定于连接到产品ID的子的SQL查询。

或者有更好的方法来解决这个问题?

这些是我可以使用的数据库表:

wp_2_posts
ID      post_title      post_status     post_type           post_parent
3557    CS 01           (not used)      product             (not used)
4154    (not used)      wc-active       shop_subscription   4153
4153    (not used)      (not used)      shop_order          (not used)
wp_2_wc_order_product_lookup
order_id    product_id
4153        3557

下面是第一个示例的PHP文件(这不是按照每个产品组织的,而是从查询中产生所有可能的组合):

$boder = array();
$bod_sql = "
SELECT
A.ID AS bod_id, A.post_title AS bod_navn, B.order_id,
C.ID AS sub_id, C.post_status AS sub_status
FROM wp_2_posts A
LEFT JOIN wp_2_wc_order_product_lookup B ON A.ID = B.product_id
LEFT JOIN wp_2_posts C ON C.post_parent = B.order_id
WHERE A.post_type = 'product'
ORDER BY A.ID";
$bod_res = mysqli_query($conn,$bod_sql) or die("Last error: {$conn->error}n");
while($bod_row = $bod_res->fetch_assoc()) {
$boder[] = $bod_row;
}
$json = json_encode($boder, JSON_PRETTY_PRINT);
echo $json;

这是我尝试的PHP文件(这是它覆盖subs的地方):

$boder = array();
$bod_sql = "
SELECT
A.ID AS bod_id, A.post_title AS bod_navn, B.order_id,
C.ID AS sub_id, C.post_status AS sub_status
FROM wp_2_posts A
LEFT JOIN wp_2_wc_order_product_lookup B ON A.ID = B.product_id
LEFT JOIN wp_2_posts C ON C.post_parent = B.order_id
WHERE A.post_type = 'product'
ORDER BY A.ID";
$bod_res = mysqli_query($conn,$bod_sql) or die("Last error: {$conn->error}n");
while($bod_row = $bod_res->fetch_assoc()) {
$boder[$bod_row['bod_id']] ['bod_id'] = $bod_row['bod_id'];
$boder[$bod_row['bod_id']] ['bod_navn'] = $bod_row['bod_navn'];
$boder[$bod_row['bod_id']] ['subs'] ['sub_id'] = $bod_row['sub_id'];
$boder[$bod_row['bod_id']] ['subs'] ['sub_id'] ['sub_status'] = $bod_row['sub_status'];
}
$json = json_encode($boder, JSON_PRETTY_PRINT);
echo $json;

如前所述,json对象上只显示一个sub_id的原因是关联数组键在PHP中是唯一的。(顺便说一句,JSON语法似乎也有点不正确)。

要获得与您提供的结果类似的结果,您可以使用以下命令:

while($bod_row = $bod_res->fetch_assoc()) {
$boder[$bod_row['bod_id']] ['bod_id'] = $bod_row['bod_id'];
$boder[$bod_row['bod_id']] ['bod_navn'] = $bod_row['bod_navn'];

// Here we create an empty array for subs if that is not yet done.
if (!isset($boder[$bod_row['bod_id']]['subs'])) {
$boder[$bod_row['bod_id']]['subs'] = [];
}
// Then we push associative arrays of key value pairs
// for each of the id and status pairs.
$boder[$bod_row['bod_id']]['subs'][] = [
'sub_id' => $bod_row['sub_id'],
'sub_status' => $bod_row['sub_status']
];
}
这将导致以下JSON输出:
{
"3557":{
"bod_id":"3557",
"bod_navn":"CS 01",
"subs":[
{
"sub_id":"4145",
"sub_status":"wc-active"
},
{
"sub_id":null,
"sub_status":null
},
{
"sub_id":null,
"sub_status":null
}
]
}
}

详细说明一下,PHP将关联数组转换为基本json对象。要保存多个对象,你真的需要一个数组来存储它们的集合,否则它会覆盖/重新分配一个值给属性。

最新更新