Remove duplicate values ​​from an array

I have an array that looks like

Array ( [0] => stdClass Object ( [post_date] => 2017-01-04 14:28:00 ) [1] => stdClass Object ( [post_date] => 2017-01-05 11:06:00 ) [2] => stdClass Object ( [post_date] => 2017-02-04 14:28:00 ) [3] => stdClass Object ( [post_date] => 2017-02-04 14:34:00 ) ) 

I used the substr () function to get the specific part -

 foreach($unique_dates as $val) { $year=substr($val->post_date,0,4); $month=substr($val->post_date,5,2); $monthName = date('F', mktime(0, 0, 0, $month, 10)); echo $monthName." ".$year."<br>"; } 

Now it shows the output as -

 January 2017 January 2017 February 2017 February 2017 

But I want Output like -

  January 2017 February 2017 

I tried as follows, but did not get the proper output -

 $unique_dates = array_map( 'unserialize', array_unique( array_map( 'serialize', $dates ) ) ); 

I want to remove duplicate values.
How is this possible?
Thank you

+4
source share
1 answer

execute it as follows:

 foreach($unique_dates as $val) { $year=substr($val->post_date,0,4); $month=substr($val->post_date,5,2); $monthName = date('F', mktime(0, 0, 0, $month, 10)); $result[] = $monthName." ".$year; } $result = array_flip($result); // here will remove the duplicate value, and return as the keys of the array. array_walk($results, function($v, $k){echo $k.'<br>';}); 
+1
source

Source: https://habr.com/ru/post/1262333/


All Articles