skip to Main Content

I have a multidimensional array storing discounted price and discount type. Here’s a basic example:

$discountNew[0]['price'] = 10;
$discountNew[0]['type'] = 'happy hour discount';
$discountNew[1]['price'] = 8;
$discountNew[1]['type'] = 'profile discount';
$discountNew[2]['price'] = 16;
$discountNew[2]['type'] = 'category discount';
$discountNew[3]['price'] = 6;
$discountNew[3]['type'] = 'medical discount';

I need a way to return the lowest price – and the associated discount type. In the above example that would be ‘6’ and ‘medical discount’.

I can find the lowest price by using min and array_column functions, like so:

$lowestPrice = min(array_column($discountNew, 'price'));

However, how can I return the discount type? I figured if I can find the key value of the original array (in the example above that would be 3), I can use that to look up the discount type. However, I can’t find a way to return the original key, as array_column produces a new array.

As such, the question would be: How can I return the discounted price, plus either the discount type or the original array key?

Please help!

Thank you.

4

Answers


  1. If the prices are unique, then get the key for the lowest price and use it:

    $lowestPriceType = $discountNew[array_search(min($price = array_column($discountNew, 'price')), $price)]['type'];
    

    Broken down:

    $prices = array_column($discountNew, 'price')
    $lowestPrice = min($prices);
    $key = array_search($lowestPrice, $prices);
    $lowestPriceType = $discountNew[$key]['type'];
    
    Login or Signup to reply.
  2. This will find it for you

    $discountNew[0]['price'] = 10;
    $discountNew[0]['type'] = 'happy hour discount';
    $discountNew[3]['price'] = 6;
    $discountNew[3]['type'] = 'medical discount';
    $discountNew[1]['price'] = 8;
    $discountNew[1]['type'] = 'profile discount';
    $discountNew[2]['price'] = 16;
    $discountNew[2]['type'] = 'category discount';
    
    
    $lowestPrice = min(array_column($discountNew, 'price'));
    
    foreach ( $discountNew as $dis ){
        if ( $dis['price'] == $lowestPrice ) {
            $type = $dis['type'];
            break;
        }
    }
    echo $type;
    
    Login or Signup to reply.
  3. $discountNew = [
      [ 'price' => 10, 'type' => 'happy hour discount' ],
      [ 'price' => 8, 'type' => 'profile discount' ],
      [ 'price' => 16, 'type' => 'category discount' ],
      [ 'price' => 6, 'type' => 'medical discount' ],
    ];
    
    $result = array_reduce(
      $discountNew,
      fn($carry, $item) => $item['price'] < $carry['price'] ? $item : $carry,
      [ 'price' => max(array_column($discountNew, 'price')) + 1, 'type' => '' ]
    );
    
    Login or Signup to reply.
  4. You can solve this by problem solving technique as well

    $min = PHP_INT_MAX;
    $min_key = 0;
    foreach ($discountNew as $key => $discount) {
       if ($discount['price'] <= $min) {
            $min = $discount['price'];
            $min_key = $key;
       }
    }
    $min_discount_type = $discountNew[$min_key]['type'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search