2017-02-24 80 views
0

我想按字母對菜單進行分組,並將它們按列分組。所以我有一個關聯數組,其中我的項目已經按字母排序。按列排序菜單項

foreach ($category['children'] as $children) { 
    $firstChar = $children['name'][0]; 


    // if character is a number set it as # 
    if (is_numeric($firstChar)) { 
    $firstChar = "#"; 
    } 

    if ($firstChar !== $previousChar) { 
    echo '<li class="submenu_tag">'.strtoupper($firstChar).'</li>'; 
    $previousChar = $firstChar; 
    } 

    echo '<li><a href="'.$children['href'].'">'.$children['name'].'</a></li>'; 
    $count++; 
    if ($count == 11) { 
    echo '</ul></div>'; 
    echo '<div><ul>'; 
    $count = 0; 
    } 
} 
echo '</ul></div>'; 

我現在會輸出這個。

<div> <div>  <div> 
A  Cow  Fawn 
Apple Crab  Fish 
Ape    Fox 
Ant  D   
     Dog  G 
B     Gazette 
Boy  E   Goose 
Ball Elephant Gorilla 
Bad  Egg   
     Elk  H 
C     Hedgehog 
Camel F   Hen 
Cat  Falcon  
</div> </div>  </div> 

期望的結果是,如果一組中屬於以下幾個項目將要超過11行(不含之間的空白空間),那麼它就會將它移動到一個新的div。事情是這樣的:

<div> <div>  <div> 
A  C   F 
Apple Cat  Falcon 
Ape  Cow  Fawn 
Ant  Crab  Fish 
        Fox 
B  D   
Boy  Dog  G  
Ball    Gazette 
Bad  E   Goose  
     Elephant Gorilla  
     Egg 
     Elk  ..... 
</div> </div>  </div> 
+0

你可以給一個示例輸入! –

+0

它只是'$ children ['id']''$ children ['name']'和'$ children ['href']' – John

回答

0

試試這個代碼

function formatData($datas) 
{ 
    $outputs = []; 
    $previousChar = ""; 
    foreach ($datas as $data) { 
     $firstChar = $data['name'][0]; 
     if (is_numeric($firstChar)) { 
      $firstChar = "#"; 
     } 

     if ($firstChar !== $previousChar) { 
      $previousChar = $firstChar; 
      $outputs[$firstChar] = []; 
      //keep Key 
      $outputs[$firstChar][] = [ 
       'name' => $firstChar, 
      ]; 
     } 

     $outputs[$firstChar][] = $data; 
    } 
    return $outputs; 
} 

function crateItemHtml($item) 
{ 
    $output = '<li>'; 
    if (isset($item['href'])) { 
     $output .= '<a href="'.$item['href'].'">'; 
    } 
    if (isset($item['name'])) { 
     $output .= $item['name']; 
    } 
    if (isset($item['href'])) { 
     $output .= '</a>'; 
    } 
    $output .= '</li>'; 
    return $output; 
} 

$datas = formatData($categories); 

$maxRow = 11; 
$count = 0; 
if (!empty($datas)) { 

    echo '<div style="width:150px; float:left;"><ol>'; 
    foreach ($datas as $data) { 
     $totalGroup = count($data); 

     if (($count+$totalGroup) >= $maxRow) { 
      //create space 
      while ($count < $maxRow) { 
       $count++; 
       //add space 
       echo crateItemHtml([]); 
      } 
      $count = 0; 
      echo '</ol></div>'; 
      echo '<div style="width:150px; float:left;"><ol>'; 
     } 

     //can add in row 
     foreach ($data as $category) { 
      $count++; 
      echo crateItemHtml($category); 
     } 
     if ($count < $maxRow) { 
      $count++; 
      //add space 
      echo crateItemHtml([]); 
     } else { 
      $count = 0; 
      echo '</ol></div>'; 
      echo '<div style="width:150px; float:left;"><ol>'; 
     } 

    } 

    echo '</ol></div>'; 
} 

,你可以在這行

$ MaxRow的= 11更改最大行;