Sort words with accents in PHP

6

I am trying to alphabetize an array in PHP, where the key of each array position is a word.

I'm using ksort . Sorting works, the problem is that accented words such as "acid" are placed at the bottom of the list.

I need to make the accented letters be ordered in the same way as letters that are not accented and that the procedure does not require much processing.

    
asked by Tércio Garcia 09.10.2014 в 18:27
source

2 answers

8

Test like this, using:

$palavras = array('ambiente', 'anão', 'anã', 'pai', 'país', 'ácido');
function compareASCII($a, $b) {
    $at = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
    $bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
    return strcmp($at, $bt);
}

uasort($palavras, 'compareASCII');
var_dump($palavras);

The result is:

array(6) {
  [5]=>
  string(6) "ácido"
  [0]=>
  string(8) "ambiente"
  [2]=>
  string(4) "anã"
  [1]=>
  string(5) "anão"
  [3]=>
  string(3) "pai"
  [4]=>
  string(5) "país"
}

Source: link

This function uasort(<array>, <função>) accepts as second parameter another function to compare array values.

    
answered by 09.10.2014 / 18:41
source
1

The solution that Sérgio pointed out did not work for me, since, for some reason, she put the words beginning with an accent before the rest of the words:

array(4) {
    ["Ética_Profissional"]=>
    array(2) {
        [0]=>
        int(12864)
        [1]=>
        int(12862)
    }
    ["Computação_Aplicada"]=>
    array(1) {
        [0]=>
        int(12861)
    }
    ["Geologia_Ambiental_e_Recursos_Hídricos"]=>
    array(1) {
        [0]=>
        int(11803)
        }
    ["Socioeconomia_e_Sustentabilidade"]=>
    array(1) {
        [0]=>
        int(12858)
    }
}

However, I found in this post a solution that worked for me:

$key_values = array(
    'Geologia_Ambiental_e_Recursos_Hídricos'    => array( 11803 ),
    'Computação_Aplicada'                       => array( 12861 ),
    'Socioeconomia_e_Sustentabilidade'          => array( 12858 ),
    'Ética_Profissional'                        => array( 12864, 12862 ),
);
function comparar_palavras($name1,$name2){
    $patterns = array(
        'a' => '(á|à|â|ä|ã|Á|À|Â|Ä|Ã)',
        'e' => '(é|è|ê|ë|É|È|Ê|Ë)',
        'i' => '(í|ì|î|ï|Í|Ì|Î|Ï)',
        'o' => '(ó|ò|ô|ö|õ|Ó|Ò|Ô|Ö|Õ)',
        'u' => '(ú|ù|û|ü|Ú|Ù|Û|Ü)'
    );          
    $name1 = preg_replace(array_values($patterns), array_keys($patterns), $name1);
    $name2 = preg_replace(array_values($patterns), array_keys($patterns), $name2);          
    return strcasecmp($name1, $name2);
}
uksort($key_values, "comparar_palavras");

The result is:

array(4) {
    ["Computação_Aplicada"]=>
    array(1) {
        [0]=>
        int(12861)
    }
    ["Ética_Profissional"]=>
    array(2) {
        [0]=>
        int(12864)
        [1]=>
        int(12862)
    }
    ["Geologia_Ambiental_e_Recursos_Hídricos"]=>
    array(1) {
        [0]=>
        int(11803)
    }
    ["Socioeconomia_e_Sustentabilidade"]=>
    array(1) {
        [0]=>
        int(12858)
    }
}
    
answered by 17.09.2017 в 23:06