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
If the prices are unique, then get the key for the lowest price and use it:
Broken down:
This will find it for you
You can solve this by problem solving technique as well