天天看點

php 删除重複值的數組,php – 從數組中删除重複值

我有一個看起來像的數組 –

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

)

)

我使用substr()函數來擷取特定的部分 –

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."

";

}

現在它顯示輸出像 –

January 2017

January 2017

February 2017

February 2017

但是我希望輸出像 –

January 2017

February 2017

我嘗試如下但沒有得到适當的輸出 –

$unique_dates = array_map(

'unserialize',

array_unique(

array_map(

'serialize',

$dates

)

)

);

我想删除重複值.

這怎麼可能?

謝謝.

解決方法:

像這樣運作:

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.'

';});

标簽:php,arrays

來源: https://codeday.me/bug/20190706/1392705.html