2009-04-30 2 views
0

어쩌면 나는 나쁜 날을 보내고 있습니다.하지만이 2 가지 조건을 1로 결합 할 수는 없습니다. 출력해야하기 때문에 분명히 있어야합니다. 똑같은 데이터. 다음 코드는 앵커 링크로 작동 할 A-Z가있는 목록을 만든 다음 'word'=>'definition' 형식으로 배열을 트래버스하기위한 것입니다. PHP가 새로운 문자를 발견 할 때마다 현재의 문자로 h3 요소를 출력하기를 원했습니다. 여기 이 목록 항목이 새 문자로 시작하는지 확인하는 데 이러한 조건을 결합하는 데 도움이 될 것입니다

내가이 26 지수는 (분명히 26 개 문자 인 오버 플로우 때문에 나는

if ($botanyWord == key(array_slice($content, 0, 1)) || substr($botanyWord, 0, 1) != $letters[$currentLetter]) 

가 잘 작동하지 않았고, error'd을하려고 노력

$letters = range('A', 'Z'); 
    echo '<em>Jump to:</em><ul class="letters">'; 
    // anchors 
    foreach ($letters as $letter) {    
     echo '<li><a href="#botany-words-' . $letter . '">' . $letter . '</a></li>';  
    }   
    echo '</ul>';   
    // start listing them!   
    $currentLetter = 0;   
    echo '<dl id="botany-words">'; 

    foreach($content as $botanyWord=>$definition) { 

     if ($botanyWord == key(array_slice($content, 0, 1))) { // must be first, aka 'A' 

      echo '<h3 id="botany-words-' . $letters[$currentLetter] . '">' . $letters[$currentLetter] . '</h3>'; 


     } 

     if (substr($botanyWord, 0, 1) != $letters[$currentLetter]) { // if the first character of this botany word is different to the last    
      echo '<h3 id="botany-words-' . $letters[$currentLetter] . '">' . $letters[$currentLetter] . '</h3>'; 

      $currentLetter++; 
     } 
    ?> 

     <dt><?php echo $botanyWord; ?></dt> 
     <dd><?php // echo $definition; ?></dd> 

    <? 
    } 
    echo '</dl>'; 

매번 무슨이다 알파벳으로).

감사합니다.

답변

1
$currentLetter = ''; 
foreach($content as $botanyWord=>$definition) { 
    if ($currentLetter !== $botanyWord[0]) { 
    $currentLetter = $botanyWord[0]; 
    // next letter ...if your array is in lex. order 
    ... 
    } 
    ... 
}
+0

안녕하세요, 구문과 같은 배열을 사용하여 문자에 액세스 할 수 있습니다. 감사합니다 VolkerK – alex

관련 문제