-2
Good evening, I need some help with the code below:
- Inside the variable '$data' I have an array where the id repeats with different dates, I need to discard one of the two arrays repeated with the same 'id', keeping the lowest date, IE, the end result would be:
"Array ( [0] => Array ( [id] => 12 [date] => 2020-07-02 ) , [1] => Array ( [id] => 13 [date] => 2020-06-10 ) ) "
- I’ve tried everything a little at first I’m working with the idea of loop inside loop, to sweep and compare, but as you can see I’m missing something;
<?php
$dados = [array("id" =>12, "data"=>"2020-07-02"),
array("id" =>13, "data"=>"2020-06-10"),
array("id" =>13, "data"=>"2020-06-15"),
array("id" =>12, "data"=>"2020-05-12")];
$total = count($dados);
foreach($dados as $item){
for($i=1; $i < $total; $i++){
if($item['id'] == $dados[$i]['id']){
if(strtotime($item['data']) <= strtotime($dados[$i]['data'])){
unset($dados[$i]);
$dados = array_values($dados);
$total = count($dados);
}
}
}
}
print_r($dados);
Array ( [0] => Array ( [id] => 12 [data] => 2020-07-02 ) ) //resultado