展示一个列表的首字母一次


Show first letter of a list once?

我有一个目录名称列表,需要从每个名称中获得第一个字母,并在该字母组开始前显示一次,即;

what I have:


12
3
4
5
阿伯丁
阿伦德尔
Aberyswith

布里斯托尔
布莱顿
卡迪夫
考文垂

我想要什么:

#
1
2
3
4
5


阿伯丁
阿伦德尔
Aberyswith



布里斯托尔
布莱顿

C
卡迪夫
考文垂

function htmlDirList($subdirs) {
    global $z_self, $z_img_play, $z_img_lofi, $z_img_more, $z_admin,
        $z_img_down, $z_img_new, $zc;
    $now = time();
    $diff = $zc['new_time']*60*60*24;
    $num = 0;
    $dir_list_len = $zc['dir_list_len'];
    if ($zc['low']) { $dir_list_len -= 2; }
    $html = "";
    $checkbox = ($z_admin || ($zc['playlists'] && $zc['session_pls']));
    /**/
    $row = 0;
    $items = sizeof($subdirs);
    $cat_cols = "2";
    $rows_in_col = ceil($items/$cat_cols);
    if ($rows_in_col < $cat_cols) { $cat_cols = ceil($items/$rows_in_col); }
    $col_width = round(100 / $cat_cols);
    $html = "<table width='600'><tr>";
    $i = 0;
    /**/
    foreach ($subdirs as $subdir => $opts) {
        if ($row == 0) {
            $class = ($cat_cols != ++$i) ? ' class="z_artistcols"' : '';
            $html .= "<td $class valign='top' nowrap='nowrap' width='$col_width%'>";
        }
        /*$currentleter = substr($opts, 0 , 1);
        if($lastletter != $currentleter){
        echo $currentleter;
        $lastletter = $currentleter;
        }*/
        if($alphabet != substr($opts,0,1)) {
        echo strtoupper(substr($opts,0,1)); // add your html formatting too.
        $alphabet = substr($opts,0,1);
        }

        $dir_len = $dir_list_len;
        $dir = false;
        $image = $opts['image'];
        $new_beg = $new_end = "";
        if (substr($subdir, -1) == "/") {
            $dir = true;
            $subdir = substr($subdir, 0, -1);
        }
        $path_raw = getURLencodedPath($subdir);
        $href = "<a href='$path_raw";
        if (!$dir) {
            if ($zc['download'] && $zc['cmp_sel']) { $html .= "$href/.lp&amp;l=8&amp;m=9&amp;c=0'>$z_img_down</a> &nbsp;"; }
            if ($zc['play']) { $html .= "$href&amp;l=8&amp;m=0'>$z_img_play</a> &nbsp;"; }
            if ($zc['low'] && ($zc['resample'] || $opts['lofi'])) { $html .= "$href&amp;l=8&amp;m=0&amp;lf=true'>$z_img_lofi</a> &nbsp;"; }
            if ($checkbox) { $html .= "<input type='checkbox' name='mp3s[]' value='$path_raw/.lp'/> &nbsp;"; }
            $num++;
            if ($zc['new_highlight'] && isset($opts['mtime']) && ($now - $opts['mtime'] < $diff)) {
                $dir_len -= 5;
                if ($z_img_new) {
                    $new_end = $z_img_new;
                } else {
                    $new_beg = $zc['new_beg'];
                    $new_end = $zc['new_end'];
                }
            }
        }
        $title = formatTitle(basename($subdir));
        if (strlen($title) > $dir_len) {
            $ht = " title='"$title.'"";
            $title = substr($title,0,$dir_len).$opts['mtime']."...";
        } else {
            $ht = "";
        }
        if ($zc['dir_list_year']) {
            $di = getDirInfo($subdir);
            if (!empty($di['year'])) $title .= " (".$di['year'].")";
        }
        $html .= "$href'$ht>$new_beg$title$new_end</a><br />";
        $row = ++$row % $rows_in_col;
        if ($row == 0) { $html .= "</td>"; }
    }
    if ($row != 0) $html .= "</td>";
    $html .= "</tr></table>";
    $arr['num'] = $num;
    $arr['list'] = $html;
    return $arr;
}

我需要帮助才能得到工作。

下面将显示目录列表,以第一个字母作为组的开头(参见代码页以获得证明):

(假设$dirs是包含名称的数组)

$cur_let = null;
foreach ($dirs as $dir) {
    if ($cur_let !== strtoupper(substr($dir,0,1))){
        $cur_let = strtoupper(substr($dir,0,1));
        echo $cur_let."'n";
    }
    echo $dir . "'n";
}

你只需要自己添加一些格式,适合你的需要。

编辑:

#符号项下的版本分组以数字开头,可以像这样:

$cur_let = null;
foreach ($dirs as $dir) {
    $first_let = (is_numeric(strtoupper(substr($dir,0,1))) ? '#' : strtoupper(substr($dir,0,1)));
    if ($cur_let !== $first_let){
        $cur_let = $first_let;
        echo $cur_let."'n";
    }
    echo $dir . "'n";
}

这是你要找的吗?

<?php
$places = array(
'Aberdeen',
'Arundel',
'Aberyswith',
'Bath',
'Bristol',
'Brighton',
'Cardiff',
'coventry'
);
$first_letter = $places[0][0];
foreach($places as $p)
{
    if(strtolower($p[0])!=$first_letter)
    {
        echo "<b>" . strtoupper($p[0]) . "</b><br/>";
        $first_letter = strtolower($p[0]);
    }
    echo $p . "<br/>";
}
?>

打印:


阿伯丁
阿伦德尔
Aberyswith


布里斯托尔
布莱顿
C
卡迪夫
考文垂

我的方法是生成第二个数组,该数组将第一个字母与以该字母开头的名称数组相关联。

$dirs; // assumed this contains your array of names
$groupedDirs = array();
foreach ($dirs as $dir) {
    $firstLetter = strtoupper($dir[0]);
    $groupedDirs[$firstLetter][] = $dir;
}

然后,你可以在$groupedDirs上迭代以打印出列表。

<?php foreach ($groupedDirs as $group => $dirs): ?>
    <?php echo $group; ?>
    <?php foreach ($dirs as $dir): ?>
        <?php echo $dir; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

这允许两个独立任务之间的清晰分离:找出组是什么,其次,显示分组列表。通过将这些任务分开,不仅每个任务的代码更清晰,而且您可以在不同的情况下重用其中任何一部分。

使用如下代码,将其更改为按您想要的方式输出HTML:

sort($subdirs);
$count = count($subdirs);
$lastLetter = '';
foreach($subdirs as $subdir => $opts){
    if(substr($subdir,0,1) !== $lastLetter){
        $lastLetter = substr($subdir,0,1);
        echo '<br /><div style="font-weight: bold;">'.strtoupper($lastLetter).'</div>';
    }
    echo '<div>'.$subdir.'</div>';
}

编辑

刚刚意识到$subdir是关联的,做了上面的修改: