出力として必要なものに応じて、この問題の解決策がいくつかあります。ここでは、これらの2つです:
ソリューション1
最低価格(番号)を取得します:すべてが同じであれば、すべての値が(True
同じであれば
$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
チェック、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;
チェックすべての値は、あなたがこれを必要とするつもりはない場合は、することができます(同じであれば、上記の2行のコメントを削除してください)。
return count(array_unique($prices) == 1);