1

I have an array, $prices, which looks like this:

Array
(
    [0] => Array
        (
            [regprice] => 25.00
            [saleprice] => 17.00
        )

    [1] => Array
        (
            [regprice] => 
            [saleprice] => 19.00
        )

)

When using $lowest_index = array_keys($prices, min($prices));, $lowest_index returns 1, because for that index, the value [regprice] is not set.

I would like to get the index of the array where [saleprice] is the lowest. In my case, that would return 0.

2
  • Is there always a saleprice? Commented Mar 14, 2019 at 13:06
  • @Nick Thanks for your comment. Yes, saleprice is always filled. Commented Mar 14, 2019 at 13:07

1 Answer 1

4

If there is always a value in saleprice, you can adapt your code using array_column to look at only the saleprice values:

$lowest_index = array_keys(array_column($prices, 'saleprice'), min(array_column($prices, 'saleprice')));

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.