Frage

I have an array that already contains all it's values in alphabetical order:

Directory Listing:

Ahha
ah?

I just want to list the first letter each starts with above it like so:

A

___________________

Ahahhaa
Ah?

Like this

Here is code.

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

I need to print directory list according to Alphabets.

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

$letter[$alphabet] == return nothing.

php v5.5.5

War es hilfreich?

Lösung

are trying to get this?

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

result: A Ahahaha Ahow B Bwahahah C Catcw

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top