如何在字母表中显示目录列表以在其Letter中显示目录

时间:2013-11-25 05:19:08

标签: php directory-listing alphabet

我有一个数组已按字母顺序包含所有值:

目录列表:

Ahha
ah?

我只想列出每个开头的第一个字母,就像这样:

A

___________________

Ahahhaa
Ah?

喜欢这个

这是代码。

$alphabets = array('0-9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

$letter = list of directory.

foreach ($alphabets as $alphabet) { 
if ($letter[$alphabet])
echo '<a href="#letter-'.$alphabet.'">'.$alphabet.'</a>  '; 
else 
echo $alphabet.'  ';  
}

我需要根据字母表打印目录列表。

<?php
foreach ($letter[$alphabet] as $name) {
   echo $name;  
}
?>

$letter[$alphabet] == return nothing.

php v5.5.5

1 个答案:

答案 0 :(得分:0)

试图得到这个?

$alphabets = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

$letter = array("Ahahah", "Bwahahah", "Ahow", "Catcw"); // is this what letters value?

foreach ($alphabets as $alphabet) {
  echo $alphabet;
  $match = null;
  foreach($letter as $let){
    preg_match("/[$alphabet]?/is", $let, $match);
    if(isset($match[0]) && $match[0]){
       echo '<a href="#letter-'.$alphabet.'">'.$let.'</a>  '; 
    }
  }
}

结果:     Ahahaha Ahow B Bwahahah C Catcw