In php how to select unique record in a loop?

There is a snippet in the CMS, sorts displays unique entries in the cycle, the format of entries in the array like this:
2016-11-11 00:04:00
2017-01-16 00:04:00
2017-09-07 00:04:00

C date to work need is already recorded in the database line of work it is necessary as with the line as I understand it. Unique records does, all is well. But the problem is small, I need ONLY unique records BY year. BUT at the same time, the output should remain the same as in the base 2017-09-07 00:04:00. Tried in the first cycle
it
$countries[] = $partner['date'];
to change it
$countries[] = substr($partner['date'],0,4);
but damn the result changes, only prints the year 2017 I need to save the output.
Here is a snippet.
$tpl = 'tpl-datenews';
$modx->resource = $modx->getObject('modResource', 1);
$tv = $modx->resource->getTVValue('field-news');
$countries = array();

$arr = json_decode($tv, true);
foreach($arr as $partner){
 $countries[] = $partner['date'];
}
$countries = array_unique($countries);
natsort($countries);
foreach($countries as $country){
 $out .= $modx->getChunk($tpl, array('date'=>$country));
}
return $out;


What to do? Choose from an array of unique records considering only the year, but save the string result for display on a website.
April 4th 20 at 00:55
1 answer
April 4th 20 at 00:57
Solution
I would have modified and so the cycle:
$arr = json_decode($tv, true);
sort($arr);
$prew = "";
foreach($arr as $partner){
 if ($prew != substr($partner['date'],0,4)) {
 $countries[] = $partner['date'];
 $prew = substr($partner['date'],0,4);
}
}
Otherwise is also possible to modify, but then it will be not the first occurrence and the last one:
$arr = json_decode($tv, true);
foreach($arr as $partner){
 $countries[substr($partner['date'],0,4)] = $partner['date'];
}
- Treva21 commented on April 4th 20 at 01:00
@Treva21, Thank You! Works :) - emerson_Rempel16 commented on April 4th 20 at 01:03

Find more questions by tags PHP