2017-02-24 3 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

あなたは

$ maxRow = 11本のラインで最大行を変更することができ、このコード

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>'; 
} 

をお試しください。

関連する問題