2016-05-18 12 views
0

opencartでデータを操作しようとしていますが、問題があります。すべての数字が同じ場合、$ product ['price']の各キーに対してこの配列をチェックして、既存の最小値を取得する方法と、真偽値を返す方法はどうでしょうか?私はforeachループでこれをやろうとしていますが、何をすべきか分かりません。ここに私が欲しいものの大まかなアイデアがあります。Opencart製品アレイ

foreach ($this->cart->getProducts() as $product) { 

if($product['price'] == false){ //not the same 
Smallest number of $product['price'] 
}else{ 
do something else 
} 

} 

答えて

0

出力として必要なものに応じて、この問題の解決策がいくつかあります。ここでは、これらの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); 
関連する問題