how to get the nearest expiration date, means here I need to get the third one 2022-03 and ignore others & also ignore the date that comes with 0000-00
my code:
$minDate = "";
foreach (json_decode($row['items']) as $item) {
if ($item->expiration != "0000-00") {
$minDate = min($item->expiration);
}
}
dd($minDate);
Or if the data is already sorted you just get the last element? The data sample you've provided is very small so it is hard to fully understand what data you will be working with.
@Jean_ali if those are 3 different examples, then it looks like you are always getting your data sorted and you don't need to worry about sorting. So just get the last element in the array. Sorting is a very expensive operation so you should avoid it if you can.
/**
*To get the Nearest Expiration date
*@input json https://echat.date array of expiration & quantity as
*per the question
*/
function getNearestExp($items){
$items = json_decode($items); // convert the items to json object
$itemArr = [ https://e-chats.com/omegle ];
foreach($items as $item) {
// exclude the '0000-00' expiration
if($item->expiration != '0000-00'){
$itemArr[strtotime($item->expiration)] = (array)$item;
}
}
// sort the out put array to ASC order to get the smallest value 1st
ksort($itemArr);
// Re indexing the array to proper index keys
$itemArr= array_values($itemArr);
// return the first value of the array
return $itemArr[0];