4

I have an array I'm trying to asort using php. The problem is that the array has accented characters in it and needs to be sorted using "french" rules.

 cote < côte < coté < côté

I've tried many things, like using php collators, but I get the following error :

PHP Fatal error:  Class 'Collator' not found

I've also tried to set locale but it didn't do anything so I'm not sure I was doing it right, or if I need to isntall the locale. I'm a little confused.

I'm using PHP 5.2.4 if that helps. If I use asort without anything, it puts all the words with accented characters at the end.

Thanks.

6
  • I wouldn't qualify this as an answer, but it's worth while to check out: brainbell.com/tutorials/php/Sorting_With_Foreign_Languages.htm Commented May 24, 2010 at 15:08
  • 1
    cote < côte < coté < côté woah... what sort of rule is that?! Commented May 24, 2010 at 15:09
  • @Anthony, I'll check it out. @nickf Haha. Basically an accented letter is in between the unaccented letter and the next one. a < à < b. Commented May 24, 2010 at 15:16
  • that doesn't explain côte < coté < côté Commented May 24, 2010 at 15:45
  • You're right. Haha that's messed up. I got that from the php website so either it's wrong or I have it wrong. I thought it should be cote < coté < côte < côté. Quite confusing either way... Commented May 24, 2010 at 16:32

3 Answers 3

11

I ended up installing the French language pack to my server and using the following :

setlocale(LC_COLLATE, 'fr_CA.utf8');
asort($array, SORT_LOCALE_STRING);

Works for my needs...

Sign up to request clarification or add additional context in comments.

1 Comment

Just used this. Worked perfectly!
2

The Collator class is part of PHP's internationalization extension, which comes standard with PHP 5.3

Since you have 5.2.4 you'll need to install this extension in order to make use of its classes.

2 Comments

Wouldn't it be better to just upgrade to 5.3? Also I'm on ubuntu.
If upgrading to 5.3 is a option then I definitely say "go for it". Then you can start using the cool new language features like closures and late static binding.
0

for those who are in Brasil:

setlocale(LC_ALL, "pt_BR", "ptb");

Pratical Example:

function cmp($a, $b) {
return strcmp($a["first_name"], $b["first_name"]);} $docs = array(
1 => array(
    'first_name' => 'Márcia Amanda',
    'crm' => 4321,
    'job' => 'Médica',
    'sex' => 'f'
),
2 => array(
    'first_name' => 'Pedro Alexandre',
    'crm' => 6789,
    'job' => 'Veterinário',
    'sex' => 'm'
),
3 => array(
    'first_name' => 'Lívia Pereira',
    'crm' => 8765,
    'job' => 'Obstetra',
    'sex' => 'f'
));  usort($docs, "cmp", SORT_LOCALE_STRING);

                            $qtas_pessoas = count($docs);

                            $j=1;

                            while (list($key, $value) = each($docs)) {

                            if ($j==1) echo "<div class='wrapper indent-bottom7-1'>";

                            $dr='';
                            if ($value["sex"]=='m') $dr='Dr.';
                            else $dr='Dra.';

                            echo "
                            <div class='grid_4 alpha'>
                                <h6 class='p2'>$dr ".$value["first_name"]."<br/>CRM ".$value["crm"]."</h6>
                                ".$value["job"]."
                            </div>\n
                            ";
                           $j++;
                           if ($j>$qtas_pessoas) {
                               echo "</div>";
                               break; // TEMOS APENAS X PESSOAS...
                           }
                           // quebrar sempre de 3 em 3
                           if ($j % 3 == 1) echo "</div><div class='wrapper indent-bottom7-1'>"; 
                            }

by Luy Angelino

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.