[英]Opencart products array
我試圖在opencart中處理一些數據,但遇到了一些麻煩。 我將如何檢查$ product ['price']每個鍵的數組,以獲取最小的現有數字以及如果所有數字均相同則為true / false布爾值。 我試圖用一個foreach循環來做到這一點,但不知道該怎么辦。 這是我想要的粗略想法。
foreach ($this->cart->getProducts() as $product) {
if($product['price'] == false){ //not the same
Smallest number of $product['price']
}else{
do something else
}
}
根據您所需要的輸出,有兩種解決方案。 這是其中兩個:
解決方案1
獲得最低價格(一個數字):
$prices = array();
foreach ($this->cart->getProducts() as $product) {
array_push($prices, $product['price']);
}
return min($prices); // return or do w/e with the lowest price
檢查所有值是否相同(如果所有值相同,則為True
否則為False
):
return count(array_unique($prices) == 1);
我們在這里計算唯一條目的數量( array_unique刪除所有重復項)並檢查其是否等於1(=全部相同)。
解決方案2
獲得價格最低的產品(對象):
$prices = array(); // necessary for the "all equals check"
foreach($this->cart->getProducts() as $product) {
if(!isset($cheapestProduct) || $cheapestProduct['price'] > $product['price']) {
$cheapestProduct = $product;
}
array_push($prices, $product['price']); // necessary for the "all equals check"
}
return $cheapestProduct;
檢查所有值是否都相同(如果您不需要此值,可以刪除上面的兩條注釋行):
return count(array_unique($prices) == 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.