How to sort array in decreasing order?

3

I have the following problem, after doing a array 'push, I want to return the amount of repetition of a term for this I did:

$key = str_replace(array(',','.','https','/','#','!','@','&','?','\',':','\'','”','(',')', 'ª','º','1','2','3','4','5','6','7','8','9','0','“','"','                    
asked by anonymous 14.09.2016 / 23:49

2 answers

2

Use the array_count_values () together with the arsort () .

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
    'pear', 'kiwi', 'kiwi', 'kiwi');

$output = array_count_values($array);
arsort($output);

print_r($output);

Output

Array
(
    [kiwi] => 3
    [apple] => 2
    [pear] => 2
    [orange] => 1
    [banana] => 1
)

Note: I used a response as a reference for SO and adapted to your issue.

    
15.09.2016 / 00:00
2

Two things:

  • The rsort throws out the keys of the original array, and you seem to want to keep the keys. This will resolve using true .

  • Therefore:

    arsort($contagem); // reordena in-place como o rsort, mas mantendo as chaves
    foreach ($contagem as $key => $value) 
    {
        echo "$key => $value,";
    }  
    
        
    14.09.2016 / 23:59