0

I am sorry if my title is incorrect, was not sure what these array values are called.

I am doing an api call that returns data, i want to use that data. Now in the data it is possible to have up to 20 'offers'. Each has their own price. I would like to return the lowest price

The structure of the results are results -> 0 -> offers -> "number from 0-19" -> price So each offer (with number 0-19) will have a price.

Is there an easy way to grab all of that data at once and just output the lowest price?

$price = $price_array['results'][0]['offers']['*can i cycle this part*']['price'];
2
  • 1
    What have you tried so far? Could "cycle this part" be solved through a simple, but really common foreach? Commented Oct 15, 2019 at 7:39
  • Do : $minPrice = min(array_column($price_array['results'][0]['offers'],'price'));echo $minPrice; Commented Oct 15, 2019 at 7:44

2 Answers 2

2
foreach ($price_array['results'][0]['offers'] as $offer) {
    echo $offer['price'];
    // and do what you want
}

Or maybe:

$minPrice = min(array_column($price_array['results'][0]['offers'], 'price'));
echo $minPrice;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_column and min().

$price = min(array_column($price_array['results'][0]['offers'],'price'));

This will return the lowest price in that column of the array.

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.