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

Multidimensional array is overwriting instead of appending

我有一个带有 Woocommerce 订阅的 Wordpress 网站。我想列出所有产品,在每个产品中我想列出每个订阅(如果有)和订阅状态(有效、已取消、暂停等)。我已经成功地列出了一个数组中的所有产品,但我在组织它时遇到了麻烦。这是一个示例片段:

[
    {
        "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”,但除了产品与产品中每个订阅之间的 link 之外,不需要它。

使用上面的示例,我希望结果或多或少像这样(附加的子项): (也不是真的想要 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 不断被覆盖,只显示最后一行。我有 2 个理论来解决这个问题,但我不知道如何正确执行它们。

  1. 运行 一个带有 subs 的单独数组,然后以某种方式合并。
  2. 运行 SQL 特定于 while 循环内连接到产品 ID 的订阅者的查询。

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

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

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 对象。要保存多个对象,您确实需要一个可以存储它们集合的数组,否则它会将 override/re-assign 的值转换为 属性.