[Typo3-dev] Splitting array acording to alphabet

Markus Witte markus.witte at native-instruments.de
Fri Jun 17 16:40:46 CEST 2005


Hello Georg,

first of all, you'd need a list with all the letter-ranges you want  
to have like
$aLetterRanges = array('ABC','DEF',....)
This also gives you the freedom to decide how you want to deal with  
umlauts etc.

Furthermore, I'd strongly advise to put the last name you use to sort  
by as the key the array, like:
$aNameList[lastname] = array(whatever properties);
if that's impossible, you'd need some kind of transformation array

As far as I can see, the first dimension in your array is some kind of
$aNameList[] = array(count=>array(lastname=>array(whatever properties)))
With this structure, it is hard to find the lastname in each row


Now you can either use a loop in a loop (outer: letterRanges, inner:  
names)
or build a new names array. The latter would look like this:

$aSortedNames = array();
foreach($aNameList as $sLastname=>$aNameProperties) {
     $iRange = floor(ord((strtoupper(substr($sLastname,0,1)))-1)/3);
     if(!array_key_exists($iRange,$aSortedNames)) {
         $aSortedNames[$aRange] = array();
     }
     $aSortedNames[$iRange][] = $aLastname;
     // or, if you want to have all the properties with you:
     // $aSortedNames[$iRange][] = array($aLastname=>$aNameProperties);
}


Now, you have your names sorted in Letter-Ranges of threes, should be  
ease to print the letter range descriptions (ABC, DEF etc) above them:
$sRange = chr($iRange*3-1).chr($iRange*3).chr($iRange*3+1);

best,
markus



-- 

| Markus Witte             | Head of Online Department
| NATIVE INSTRUMENTS GmbH  | markus.witte at native-instruments.de
| Schlesische Str. 28      | http://www.native-instruments.de
| D-10997 Berlin           | Tel. +49/30/61 10 35-1531
| Germany                  | Fax  +49/30/61 10 35-2531
|
| >>>> NATIVE INSTRUMENTS - Generate the future of sound! <<<<

Am 17.06.2005 um 08:54 schrieb Georg Ringer:

> Hello,
>
> now I have really got a hard nut to crack:
> I havw got a muldidimensional array which looks like this:
> [count] [last name] [first name] [id] which is sorted by last name.
>
> Now I want to show before every third alphabetic caracter a varible.
> Example list:
> Auerbach, Bloch, Brecht, Caspar, Frisch, Gabor, Gandhi, Hauschka,  
> Hesse,
> Kant, Lichtenberg, Loren, Marshall,.....
>
> The result should be:
> ABC: Auerbach, Bloch, Brecht, Caspar,
> DEF: Frisch
> GHI: Gabor, Gandhi, Hauschka, Hesse
> JKL: Kant, Lichtenberg, Loren
> MNO: Marshall
>
> Can somebody please help me with this problem or can tell me how I can
> resolve it?
>
> many many thanks
> Georg
>
>
> _______________________________________________
> Typo3-dev mailing list
> Typo3-dev at lists.netfielders.de
> http://lists.netfielders.de/cgi-bin/mailman/listinfo/typo3-dev
>





More information about the TYPO3-dev mailing list