Sort an array with special characters in php

I have an array that I am trying to execute using php. The problem is that there are accented characters in the array, and they need to be sorted using the "French" rules.

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

I tried many things, for example using php-collators, but I get the following error:

 PHP Fatal error: Class 'Collator' not found 

I also tried to set the locale, but did nothing, so I'm not sure if I am doing it right, or if I need the locale not to be. I'm a little confused.

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

Thanks.

+3
source share
3 answers

I finished installing the French language pack on my server and used the following:

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

Works for my needs ...

+11
source

The Collator class is part of the PHP internationalization extension that ships with PHP 5.3.

Since you have 5.2.4, you need to install this extension in order to use its classes.

+2
source

for those located in Brazil:

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

Right 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'>"; } 

Louis Angelino

0
source

All Articles