【发布时间】:2018-11-17 06:53:12
【问题描述】:
我有一个这样填充的数组:
array_push($comboUserPosts,
array(
'link'=> get_permalink(),
'dates'=> $value,
'title'=> get_the_title()
)
);
然后当我完成所有循环时,我会这样做:
array_unique($comboUserPosts);
但它仍然给我重复。如果我这样做:
echo '<pre>' . var_export($comboUserPosts, true) . '</pre>';
这是我得到的:
array (
0 =>
array (
'link' => 'https://example.com/test/test-values-users/',
'dates' => '1920',
'title' => 'test values users',
),
1 =>
array (
'link' => 'https://example.com/test/test-values-users/',
'dates' => ' 1954',
'title' => 'test values users',
),
2 =>
array (
'link' => 'https://example.com/test/provo-filter/',
'dates' => '1600',
'title' => 'provo filter',
),
3 =>
array (
'link' => 'https://example.com/test/provo-filter/',
'dates' => '1450',
'title' => 'provo filter',
),
4 =>
array (
'link' => 'https://example.com/test/provo-filter/',
'dates' => '1330',
'title' => 'provo filter',
),
)
但是通过查看,我只有 2 个唯一链接和标题,我应该只显示这两个链接+标题:
foreach ($comboUserPosts as $value) { ?>
<h2><a href="<?php echo $value['link']; ?>"><?php echo $value['title']; ?></a>
<?php }
【问题讨论】:
-
dates呢? -
@RB 刚刚按照那个问题尝试了
array_unique($comboUserPosts, SORT_REGULAR);并且仍然...... -
@C2486 我不关心日期,我只需要两个唯一的链接和标题..
-
其实
array_unique不适合多维数组。这可能会有所帮助:https://stackoverflow.com/a/10514539
标签: php arrays associative-array