Using WordPress and PHP, I’m creating a list of tags on a site sorted by their first character,
<?php
$letters = range( 'a','z' );
array_push( $letters, 'å', 'ä', 'ö' );
foreach ( $letters as $index=>$letter ) : ?>
<h3><?php echo $letter; ?></h3>
<ul>
<?php
$tags = get_tags( array('name__like' => $letter, 'hide_empty' => 0) );
foreach ( $tags as $tag ) :
?>
<li><a href="/tag/<?php echo $tag->slug; ?>/"><?php echo $tag->name; ?></a></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
The output works except my Swedish characters Ã¥, ä, ö are also included in a / o and vice versa, as if PHP can’t distinguish them (even when manually pushing them into the array as their own entry). Ideas?
I’ve been battling the same problem. You can use the setlocale() together with asort($array ,SORT_LOCALE_STRING). That puts the à Ãà characters at the end. Unfortunately they’re in the wrong order. PHP sorts them ä, Ã¥, ö. In nordic languages (at least in the swedish alphabet) the are sorted as follows: Ã¥, ä, ö. After a lot of googling I could not solve this with PHP native functionality so I made my own “patched” sorting that I thought I might share.
I am by no means an expert in writing code optimized for computing speed, so I am sure there are several improvements that can be made to this code. I welcome you to point them out!
This function sorts an array based on the whole strings though, and not just the first character. But I believe that’s the most useful way to do it, and If you wish to ignore all characters after the first I’m sure you can modify this. Also it’s quite easy to modify it to apply to other characters and other sorting orders. Enjoy