加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从数组中删除重复值

发布时间:2020-12-13 16:08:40 所属栏目: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 ( [p
我有一个看起来像的数组 –

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,4);
    $month=substr($val->post_date,5,2);
    $monthName = date('F',mktime(0,$month,10)); 
    echo $monthName." ".$year."<br>";
}

现在它显示输出像 –

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,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>';});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读