我有以下功能:
function getStoresDisplay($data, $page_number, $nb_display) {
$nb_stores = $data['nb_stores'];
$list = $data['list'];
$current_address = $data['address'];
if($current_address=='') $current_address_display = ' ';
else $current_address_display=$current_address;
$display .= '<ul data-role="listview" data-theme="d">';
$display .= '<li data-role="list-divider" data-theme="a">'.$current_address_display.'<span class="ui-li-count">'.$nb_stores.'</span></li>';
for($i=0; $i<count($list); $i++) {
$id = $list[$i]['id'];
$name = $list[$i]['name'];
$logo = $list[$i]['logo'];
$address = $list[$i]['address'];
$distance = $list[$i]['distance'];
$created = $list[$i]['created'];
$display .= '<li><a href="javascript:" class="displayStoreDetails" id="'.$id.'">';
if($logo!='') $display .= '<img src="'.$logo.'" style="margin-top:18px;">';
$display .= '<h3>'.$name;
$display .= '</h3>';
//if($current_address!='') $display .= '<span class="ui-li-count"><font color="red"><small>'.ceil($distance).' '.$GLOBALS['distance_unit'].'</small></font></span>';
$display .= '<p>'.$address.'</p>';
if($current_address!='') $display .= '<p><small>Dista da te: </small><font color="red"><small>'.ceil($distance).' '.$GLOBALS['distance_unit'].'</small></font></p>';
$display .= '</a></li>';
}
$display .= '</ul><br>';
$display .= '<div data-role="controlgroup" data-type="horizontal" data-theme="a" style="text-align:right;" >';
if($page_number>1) $display .= '<a href="javascript:" id="displayStoresListNextPreviousBtn" page_number="'.($page_number-1).'" data-role="button" data-icon="arrow-l" data-theme="d">Previous</a>';
$display .= '<a href="javascript:" data-role="button" data-theme="d"><span id="pageNumberReload">'.$page_number.'</span></a>';
if($nb_stores>($page_number*$nb_display)) $display .= '<a href="javascript:" id="displayStoresListNextPreviousBtn" page_number="'.($page_number+1).'" data-role="button" data-icon="arrow-r" data-theme="d">Next</a>';
$display .= '</div>';
return $display;
我希望它只打印具有唯一名称($name = $list[$i]['name'];
)的结果。
我在不同的类别中有相同的项目,所以我希望它不应该打印具有相同名称的重复项目。
我该怎么做?
答案 0 :(得分:0)
尝试
$unique_names = array();
for($i=0; $i<count($list); $i++) {
if (in_array($list[$i]['name'], $unique_names)) { continue; }
$unique_names[] = $list[$i]['name'];
...
答案 1 :(得分:0)
替换以下代码:
for($i=0; $i<count($list); $i++) {
$id = $list[$i]['id'];
$name = $list[$i]['name'];
这个:
$names = array();
for($i=0; $i<count($list); $i++) {
$id = $list[$i]['id'];
$name = $list[$i]['name'];
if (in_array($name,$names)) {
continue;
}
$names[] = $name;
将跳过循环中的重复条目并执行您想要的操作。