簡體   English   中英

如何比較具有不同項目計數的兩個多維關聯 arrays

[英]How to Compare two multidimensional associative arrays with differen items count

我有兩個具有不同項目計數的多維關聯 arrays。 重要的是我不知道哪個數組會有更多元素(A 或 B)

第一個陣列(A):

    [0] => Array
        (
            [catID] => 65
            [discount] => 10
            [productID] => Array
                (
                    [0] => 10887
                    [1] => 8508
                    [2] => 8350
                )

            [startDate] => 05/12/2022 12:00 am
            [endDate] => 10/12/2022 12:00 am
        )
   [1] => Array
        (
            [catID] => 66
            [discount] => 10
            [productID] => Array
                (
                    [0] => 13184
                    [1] => 10707
                    [2] => 8350
                )

            [startDate] => 10/12/2022 12:00 am
            [endDate] => 15/12/2022 12:00 am
        )

第二個陣列(B):

[0] => Array
    (
        [catID] => 72
        [discount] => 15
        [productID] => Array
            (
                [0] => 16239
                [1] => 16236
                [2] => 10887
                [3] => 13184
                [4] => 8524
                [5] => 13314
            )
        [startDate] => 12/12/2022 12:00 am
        [endDate] => 15/12/2022 12:00 am
    )

比較這些 arrays (A, B) 之后,我想檢索類似的內容:

數組 A(如果數組 B 中存在 productID,則將其移除):

    [0] => Array
        (
            [catID] => 65
            [discount] => 10
            [productID] => Array
                (
                    [1] => 8508
                    [2] => 8350
                )
            [startDate] => 05/12/2022 12:00 am
            [endDate] => 10/12/2022 12:00 am
        )
   [1] => Array
        (
            [catID] => 66
            [discount] => 10
            [productID] => Array
                (
                    [0] => 10707
                )
            [startDate] => 10/12/2022 12:00 am
            [endDate] => 15/12/2022 12:00 am
        )

數組 B(無變化):

[0] => Array
    (
        [catID] => 72
        [discount] => 15
        [productID] => Array
            (
                [0] => 16239
                [1] => 16236
                [2] => 10887
                [3] => 13184
                [4] => 8524
                [5] => 13314
            )
        [startDate] => 12/12/2022 12:00 am
        [endDate] => 15/12/2022 12:00 am
    )

從第二個數組填充一個平面“黑名單”數組。

遍歷第一個數組的行並根據黑名單數組過濾 productId 值。

代碼:(演示

$blacklist = array_merge(...array_column($b, 'productID'));

var_export(
    array_map(
        fn($row) => array_replace($row, ['productID' => array_values(array_diff($row['productID'], $blacklist))]),
        $a
    )
);

或者,如果您不介意經典的foreach() ,那么這可能更容易閱讀:(演示

$blacklist = array_merge(...array_column($b, 'productID'));

foreach ($a as &$row) {
    $row['productID'] = array_values(array_diff($row['productID'], $blacklist));
}
var_export($a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM