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
Post a Comment