2012-10-08 44 views
0

打印結果我有以下功能:唯一的名字

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

發佈'$ data'中的內容樣本。 –

回答

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']; 
    ... 
+0

Tnx支持,它工作良好:) – user1726972

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; 

,將跳過從循環重複的條目,做你想要的東西。