[英]Create an attributes array => values without duplicates
我在 prestashop 下開發了一個模塊,並從 API 中檢索到帶有變體的文章列表。 這是一篇文章的形式。
array:24 [▼
"is_default" => false
"options" => []
"_id" => "5dc40f2f8ec93f00089d2408"
"name" => "LONGONI noir à scratch - Gant"
"full_price" => 21
"reference" => "1301100017"
"in_stock" => true,
"variations" => array:1 [▼
0 => array:3 [▼
"priority" => 0
"key" => "main"
"values" => array:2 [▼
0 => array:2 [▼
"priority" => 0
"value" => "droite"
]
1 => array:2 [▼
"priority" => 1
"value" => "gauche"
]
]
]
]
"declinations" => array:2 [▼
0 => array:10 [▼
"taxes" => array:1 [▶]
"id" => "k2oozxne"
"reference" => "1301100017DL"
"full_price" => 21
"in_stock" => true
"deprecated" => false
"test" => false
"ignore_stock" => true
"variations" => array:1 [▶]
"stock_quantity" => 0
]
1 => array:10 [▼
"taxes" => array:1 [▶]
"id" => "k2oozxnf"
"reference" => "1301100017GL"
"full_price" => 21
"in_stock" => true
"deprecated" => false
"test" => false
"ignore_stock" => true
"variations" => array:1 [▶]
"stock_quantity" => -1
]
]
]
在每篇文章中都有“變體”,我希望能夠創建一個表格,其中包括所有變體 UNIQUE,其值為 UNIQUE。
我開始做這樣的事情,它得到了所有的變化,但沒有價值。 我希望能夠將與變化相對應的所有值注入其中。 例子:
main => [
'droite',
'gauche']
$attributes = [];
foreach ($articles as $article) {
if ($article['variations']) {
foreach ($article['variations'] as $key => $variation) {
$attrName = trim(str_replace(':', '', $variation['key']));
$attributes[] = $attrName;
}
}
}
dump(array_unique($attributes));
謝謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.