php - Unique id per first letter in array key? -


i have array looks this:

$array = array(     "aceton" => "description here",     "acetonurie" => "description here",     "adipositas" => "description here",     "bolus" => "description here",     "cataract" => "description here",     "cortisol" => "description here", ); 

next build definitionlist using array data:

<dl> <?php foreach ($array $key => $value): ?> <dt><?php echo $key; ?><dd><?php echo $value; ?> <?php endforeach; ?> </dl> 

that works fine offcourse need more. need way generate id per unique first letter, result becomes:

<dl> <dt id="a">aceton <dd>description here <dt>acetonurie <dd>description here <dt>adipositas <dd>description here <dt id="b">bolus <dd>description here <dt id="c">cataract <dd>description here <dt>cortisol <dd>description here et cetera.. </dl> 

any idea how done?

try this,

   <dl>   <?php      $tmp=array();      foreach ($array $key => $value): ?>    <dt <?php if(!in_array($key[0],$tmp))    { echo "id='".$key[0]."'"; array_push($tmp,$key[0]); } ?> >   <?php echo $key; ?>   </dt>     <dd><?php echo $value; ?></dd>   <?php endforeach; ?>   </dl> 

i got,

     <dl>      <dt id="a">aceton</dt><dd>description here</dd>      <dt>acetonurie</dt><dd>description here</dd>      <dt>adipositas</dt><dd>description here</dd>      <dt id="b">bolus</dt><dd>description here</dd>      <dt id="c">cataract</dt><dd>description here</dd>      <dt>cortisol</dt><dd>description here</dd>     </dl> 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -