2017-01-05 22 views
5

Mam tablicę, która wygląda jak-usunąć duplikaty wartości z tablicy

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 
    ) 
) 

użyłem substr() funkcji, aby uzyskać konkretnemu miesięcznej

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>"; 
} 

teraz jest pokazujący jak wyjście -

January 2017 
January 2017 
February 2017 
February 2017 

Ale chcę wyjść jak -

January 2017 
February 2017 

Próbowałem następująco ale nie dostał prawidłowego output-

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

Chcę usunąć duplikaty wartości.
Jak to jest możliwe?
Dzięki.

+1

spróbować http://php.net/manual/en/function.array-unique.php – xFighter

+0

Więc to będzie twoja tablica zrobić unikalne prawo? $ monthName. "". $ year – rahulsm

+0

@VforVendetta yeah right – Deepak

Odpowiedz

1

run to tak:

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

@ Kris przepraszam za spóźnioną odpowiedź. Właśnie tego chciałem. Dzięki za pomoc. – Deepak

+0

@Deepak OK, jeśli to działa, proszę, zaakceptuj to. –