![](/img/trans.png)
[英]PHP Check if Key Exists in Multidimensional Array and Object Combination then Get Value
[英]How to check if this combination exists in the array
我正在創建一個具有變化組合的產品,但我遇到了重復屬性但不同 sku 的問題。
我有一個這樣的數組
$variations = [
"PRODUCT-SKU-1" => [
["option" => "color", "term": "black"],
["option" => "size", "term": "small"],
],
"PRODUCT-SKU-2" => [
["option" => "color", "term": "white"],
["option" => "size", "term": "small"],
]
];
現在我試圖找到一種方法來避免具有相同屬性的重復。 輸入數組的前例
$newVariant = [
"sku" => "PRODUCT-SKU-3",
"attributes" => [
["option" => "color", "term": "white"],
["option" => "size", "term": "small"]
]
]
所以基本上這個$newVariant
變量與PRODUCT-SKU-2
具有相同的屬性
編輯:
所以基本上我想檢查$variations
是否已經存在這兩個屬性組合
您可以將 2 個數組與 == 運算符進行比較。 它比較所有鍵和值是否相同:
https://www.php.net/manual/en/language.operators.array.php
您還可以使用 in_array() 來檢查數組數組是否包含子數組。
https://www.php.net/manual/en/function.in-array.php
if (in_array($newVariant, $variations){
// contains!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.