Re: array.sorting.php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Sep 4, 2012 at 7:38 PM, John Taylor-Johnston
<jt.johnston@xxxxxxxxxxxxxx> wrote:
>
> David OBrien wrote:
>>
>> On Tue, Sep 4, 2012 at 1:15 PM, John Taylor-Johnston
>> <jt.johnston@xxxxxxxxxxxxxx <mailto:jt.johnston@xxxxxxxxxxxxxx>> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux