questions

json data, get the lowest price, but dont know parent label

Rate this post

I am looking to get the lowest price from some json data, I get the info from an api call, and json_decode it.

The format is: products productnumber -> price

$product_array[‘products’][..changingnumber..][‘value’]

I dont know the productnumber (and don’t need it either). I do want find the lowestprice for all productnumbers combined

$price = min($product_array['products'][..changingnumber..]['value']);

Is there a way to do this? or is there some documentation that can help with this?

 

✔️Solution:

First map the ‘value’-s, transform them into a simple array containing all the prices:

$all_prices = array_map(function($item) {
    return $item['value'];
}, $product_array['products'])

Then just choose the minimum:

$price = min($all_prices);

Or combined into a single statement:

$price = min(array_map(function($item) {
    return $item['value'];
}, $product_array['products']));

Leave a Reply

Your email address will not be published.

Back to top button