Phpnewb
Phpnewb

Reputation: 137

Php Array, cycle through a 'parent'

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'];

Upvotes: 0

Views: 38

Answers (2)

Andreas
Andreas

Reputation: 23958

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.

Upvotes: 1

u_mulder
u_mulder

Reputation: 54831

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;

Upvotes: 2

Related Questions