ARTICLE AD BOX
I know that seasoned PHP would do this easily. My background is more from Obj-C/Swift, and I haven't been able to come up with an elegant/optimal solution to this issue without iterating three times and then compare. I suspect this could be done more easily. So, I have a JSON that I need to go through and find the lowest value, and then keep them along with two more values (ship_id and service_id) from that same child to access them later:
{ "data": { "id": "038949025", "rates": [ { "ship_id": "9", "service_id": "11", "charges": [ { "type": "early", "total": 215.66 }, { "type": "late", "total": 235.38 } ] }, { "ship_id": "9", "service_id": "12", "charges": [ { "type": "early", "total": 128.85 }, { "type": "late", "total": 148.57 } ] }, { "ship_id": "10", "service_id": "5", "charges": [ { "type": "early", "total": 121.50 }, { "type": "late", "total": 118.37 } ] } ] } }I don't think my current code is relevant here since it's two iterations where I create two separate arrays and define the totals as keys for one of them and then nest another foreach to extract the lowest price.
So, what I need to do is to go through the JSON and identify the cheapest and then save the associated ship_id and service_id values to use them afterwards. I've tried to use min() but without success. I hope this is clear enough, if not, apologies, I can always edit and clarify if needed.
EDIT: I think I should've added the expected output and that would make more sense:
I need to obtain those three variables assigned with those values:
$lowest_rate 121.50 $lowest_ship_id: 10 $lowest_service_id: 5