1
\$\begingroup\$

I am writing a method to find me a price range for special price as well as the retail price. Such as in example below I have price range for retail from 129 to 329 where as special price range is 89 to 199

array
0 =>
    array
            'id' => 83739000038374
            'special-price' => 99.99
            'retail-price' => 199
1 =>
    array
            'id' => 83739000038374
            'special-price' => 199.99
            'retail-price' => 299
2 =>
    array
            'id' => 83739000038374
            'special-price' => 89
            'retail-price' => 129
3 =>
    array
            'id' => 83739000038374
            'special-price' => 179
            'retail-price' => 329

my code is

public function getPriceRange(){
    $priceRange = array();
    $showRange = true;
    if($originalArray && count($originalArray) > 1){
        foreach($originalArray as $idx => $originalArraySingle){
            if($idx == 0){
            $priceRange['price']['max'] = $originalArraySingle['price'];
            $priceRange['price']['min'] = $originalArraySingle['price'];
            $priceRange['original-price']['max'] = $originalArraySingle['original-price'];
            $priceRange['original-price']['min'] = $originalArraySingle['original-price'];
            }else{
                if($originalArraySingle['price'] > $priceRange['price']['max'] ){
                    $priceRange['price']['max'] = is_int($originalArraySingle['price']) ? number_format((float)$originalArraySingle['price'], 2, '.', '') : $originalArraySingle['price'];
                }
                if($originalArraySingle['price'] < $priceRange['price']['min'] ){
                    $priceRange['price']['min'] = is_int($originalArraySingle['price']) ? number_format((float)$originalArraySingle['price'], 2, '.', '') : $originalArraySingle['price'];
                }
                if($originalArraySingle['original-price'] > $priceRange['original-price']['max'] ){
                    $priceRange['original-price']['max'] = is_int($originalArraySingle['original-price']) ? number_format((float)$originalArraySingle['original-price'], 2, '.', '') : $originalArraySingle['original-price'];
                }
                if($originalArraySingle['original-price'] < $priceRange['original-price']['min']){
                    $priceRange['original-price']['min'] = is_int($originalArraySingle['original-price']) ? number_format((float)$originalArraySingle['original-price'], 2, '.', '') : $originalArraySingle['original-price'];
                }
            }
        }
        //doesnt make sense to show price range if its same.
        if($priceRange['price']['max']===$priceRange['price']['min'] ||  $priceRange['price']['max']===$priceRange['price']['min']){
            $showRange = false;
        }
    }
    return $showRange ? $priceRange : null;
}

I am looking for scalability and for fastest code processing.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
if($originalArraySingle['original-price'] < $priceRange['original-price']['min']){
    $priceRange['original-price']['min'] = is_int($originalArraySingle['original-price']) ? number_format((float)$originalArraySingle['original-price'], 2, '.', '') : $originalArraySingle['original-price'];
}

You could write a function for this. And one for maximize.

protected function minimize($a,&$b){
    if($a<$b){
        $b = is_int($a)?number_format((float)$a,2,'.',''):$a;
    }
}

I would also write.

if($priceRange['price']['max']===$priceRange['price']['min'] ||  $priceRange['price']['max']===$priceRange['price']['min']){
        return null;
    }

and change

return $showRange ? $priceRange : null;

to

return $priceRange;

so you have one variable less.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.