Re: array.sorting.php
| From: | Matijn Woudt | Date: | Tue, 04 Sep 2012 17:45:19 +0000 |
| Subject: | Re: array.sorting.php | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318989@lists.php.net to get a copy of this message | ||
On Tue, Sep 4, 2012 at 7:38 PM, John Taylor-Johnston
<jt.johnston@usherbrooke.ca> wrote:
>
> David OBrien wrote:
>>
>> On Tue, Sep 4, 2012 at 1:15 PM, John Taylor-Johnston
>> <jt.johnston@usherbrooke.ca
>> <mailto:jt.johnston@usherbrooke.ca>> wrote:
>>
>> Hi,
>> Sorting question.
>> http://www.php.net/manual/en/array.sorting.php
>>
>> I'm using ksort, which sorts like this.
>> I don't want to use strolower.
>> Is there a function/switch in one of the sort functions where I
>> could get another aphabetical sort?
>>
>> http://www.php.net/manual/en/array.sorting.php has a
>> lot of
>> options. I,m not sure which one I should choose.
>>
>> This is what I get:
>>
>> LABRADOR CITY
>> LACOSTA
>> LARGE
>> Labrador
>> Labrador City
>> Labrador Sea
>> Lacosta
>> Large
>> labrador
>> labrador sea
>> lacosta
>> large
>>
>>
>> This is what I want:
>>
>> LABRADOR
>> Labrador
>> labrador
>> LABRADOR CITY
>> Labrador City
>> Labrador Sea
>> labrador sea
>> LACOSTA
>> Lacosta
>> lacosta
>> LARGE
>> Large
>> large
>>
>>
>> natsort
>> natcasesort
>
> I'm sorting the key. It doesn't work. ksort is the only choice I have,
> right?
>
> Ok, let's approach this question differently.
> Right now the frequency of each word is in value.
> How do I put the frequency of each word in the key?
>
try array_flip(), keep in mind though, that when two words have the
same frequency, one of them will get lost because the keys are
identical.
> I'm fuzzy when it comes to arrays. I can do this is MySQL. All I need is an
> id and two varchars and then I know how to sort any way I want. But I am
> processing dynamic text files. MySQL is not an option; data changes too
> much. PHP is faster and more efficient. I'm just no good at arrays.
>
> Here is my code:
> ---------------snip-------------------
> $mynewstring = "http://foo.com/bigtextfile.txt";
>
> $words = preg_split('/[[:space:]]+/',$mynewstring);
>
> foreach ($words as $word) {
> $freq[$word]++;
> }
>
> #natcasesort($freq);
> ksort($freq);
>
> John
You can try uksort with a custom compare function.
- Matijn