从数组中获取唯一值

时间:2012-11-11 19:18:01

标签: php arrays multidimensional-array

我目前有一个看起来像下面那个的数组,但我只想在数组中显示显示结果是唯一的(描述),我用array_unique假设?但我一直得到相同的结果?

这是我的阵列:

$taglist = array(5) { 
[0]=> array(5) { 
    ["id"]=> string(2) "27" 
    ["page_id"]=> string(2) "18" 
    ["description"]=> string(10) "Web Design" 
    ["slug"]=> string(10) "web-design" 
    ["visibility"]=> string(7) "visible"
} 
[1]=> array(5) { 
    ["id"]=> string(2) "29" 
    ["page_id"]=> string(2) "18" 
    ["description"]=> string(3) "Tutorials" 
    ["slug"]=> string(3) "tutorials" 
    ["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
    ["id"]=> string(2) "31"
    ["page_id"]=> string(2) "21"
    ["description"]=> string(3) "tag" 
    ["slug"]=> string(3) "tag"
    ["visibility"]=> string(7) "visible"
}
[3]=> array(5) { 
    ["id"]=> string(2) "32" 
    ["page_id"]=> string(2) "21" 
    ["description"]=> string(10) "Web Design" 
    ["slug"]=> string(10) "web-design" 
    ["visibility"]=> string(7) "visible"
}
}

这是我的时间:

$items      = array();
$results    = $taglist;

foreach ($results as $result)
{
$items[]= $result['description'];
$items  = array_unique($items);
}

echo '<ul>';
while ($tag_item = current($items))
{
echo '<li><a href="/'.$page['slug'].'/tag/'.$tag_item['slug'].'/">'.$tag_item['description'].'</a></li>';
    next($items);
}
echo '</ul>';

1 个答案:

答案 0 :(得分:0)

$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);

// echo var_export($taglist, 1);  // uncomment to see the diff vs var_dump()

foreach($taglist as $tag){
$new[] = $tag['description'];

}

var_dump(array_unique($new));