Ravshan Abdurasulov Ravshan Abdurasulov - 6 months ago 10
PHP Question

how to delete similar values

Problem



I grabed all needed values from array however that contains duplicates. Now I want to remove duplicates. But I can't figure it out on how to do that.

I tried with
$uniq = array_unique($days,SORT_REGULAR)
, but that didn't work at all.

PHP



$days = " ";
foreach($data["reports"][1] as $values) {
$days .= $values['time_pay'];

}


What returns



2016-05-01 00:00:002016-05-01 00:00:002016-05-01 00:00:002016-05-01 00:00:002016-05-01 00:00:002016-05-02 00:00:002016-05-02 00:00:002016-05-02 00:00:002016-05-02 00:00:002016-05-02 00:00:002016-05-02 00:00:002016-05-03 00:00:002016-05-03 00:00:002016-05-03 00:00:002016-05-03 00:00:002016-05-03

Answer

You can simply use isset along with implode function like as

$days = [];        
foreach($data["reports"][1] as $values) {
    if(!in_array($values['time_pay'],$days)){
       $days[] = $values['time_pay'];
    }
}

echo implode($days);