Re: [PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston

That works, thanks.
I think I'll put this into MySQL eventually. "sort by" key would do it 
better, and I will need to more.


Serge Fonville wrote:
Sort does not maintain the association between key and value. Use 
asort to sort on value while maintaining the key.


Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table 





2012/9/3 John Taylor-Johnston >






Sort does not work seamlessly. I have my key and
sort($freq);
print_r ($freq);
looks like:

Array
(
...
[1000] => 172
[1001] => 176
[1002] => 179
[1003] => 441
)

This is what I want:

Array
(
...
[and] => 172
[of] => 176
[to] => 179
[the] => 441

)


http://php.net/manual/en/array.sorting.php is pretty clear. But my
problem is that sort ($freq) destroyed my key.







Re: [PHP] ksort by value

2012-09-03 Thread Stuart Dallas
On 3 Sep 2012, at 22:55, John Taylor-Johnston  
wrote:
> http://php.net/manual/en/array.sorting.php is pretty clear. But my problem is 
> that sort ($freq) destroyed my key.

Did you actually read it? There's a table on that page that summarises exactly 
what each sorting function does, with a column for whether they "maintain key 
association."

In this case you want http://php.net/asort

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] ksort by value

2012-09-03 Thread Serge Fonville
If you want to perform a count on all the unique values (in SQL Terms a
group by and a count) use
http://php.net/manual/en/function.array-count-values.php  instead.

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table



2012/9/3 Serge Fonville 

> Sort does not maintain the association between key and value. Use asort to
> sort on value while maintaining the key.
>
> Kind regards/met vriendelijke groet,
>
> Serge Fonville
>
> http://www.sergefonville.nl
>
> Convince Microsoft!
> They need to add TRUNCATE PARTITION in SQL Server
>
> https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table
>
>
>
> 2012/9/3 John Taylor-Johnston 
>
>>
>> ...
> $words = preg_split('/[[:space:]]+/',$mynewstring);
>
> foreach ($words as $word) {
>  $freq[$word]++;
> }
>
> ksort($freq);
> print_r ($freq);
> ?>
>

  Sort does not work seamlessly. I have my key and
>>> sort($freq);
>>> print_r ($freq);
>>> looks like:
>>>
>>> Array
>>> (
>>> ...
>>> [1000] => 172
>>> [1001] => 176
>>> [1002] => 179
>>> [1003] => 441
>>> )
>>>
>>> This is what I want:
>>>
>>> Array
>>> (
>>> ...
>>> [and] => 172
>>> [of] => 176
>>> [to] => 179
>>> [the] => 441
>>>
>>> )
>>>
>>
>> http://php.net/manual/en/array.sorting.php is pretty clear. But my
>> problem is that sort ($freq) destroyed my key.
>>
>>
>>
>


Re: [PHP] ksort by value

2012-09-03 Thread Serge Fonville
Sort does not maintain the association between key and value. Use asort to
sort on value while maintaining the key.

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table



2012/9/3 John Taylor-Johnston 

>
>>>> ...
 $words = preg_split('/[[:space:]]+/',$mynewstring);

 foreach ($words as $word) {
  $freq[$word]++;
 }

 ksort($freq);
 print_r ($freq);
 ?>

>>>
>>>  Sort does not work seamlessly. I have my key and
>> sort($freq);
>> print_r ($freq);
>> looks like:
>>
>> Array
>> (
>> ...
>> [1000] => 172
>> [1001] => 176
>> [1002] => 179
>> [1003] => 441
>> )
>>
>> This is what I want:
>>
>> Array
>> (
>> ...
>> [and] => 172
>> [of] => 176
>> [to] => 179
>> [the] => 441
>>
>> )
>>
>
> http://php.net/manual/en/array.sorting.php is pretty clear. But my
> problem is that sort ($freq) destroyed my key.
>
>
>


Re: [PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston


   


   Sort does not work seamlessly. I have my key and
   sort($freq);
   print_r ($freq);
   looks like:

   Array
   (
...
[1000] => 172
[1001] => 176
[1002] => 179
[1003] => 441
   )

   This is what I want:

   Array
   (
...
[and] => 172
[of] => 176
[to] => 179
[the] => 441

   )


http://php.net/manual/en/array.sorting.php is pretty clear. But my 
problem is that sort ($freq) destroyed my key.





Re: [PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston


Serge Fonville wrote:

Have you looked at http://php.net/manual/en/array.sorting.php?

2012/9/3 John Taylor-Johnston >





ksort($freq) sorts the array by the key. And that works fine.
But I would also like to sort by value to see which words
are more frequent.
There is no |ascending/descending option to ksort?|

ksort sorts by key, if you want by value, look at sort.

As to asc/desc sort, they just have a different name. ksort
and sort
are asc, krsort and rsort are desc equivalents.
- Matijn

I'm fuzzy when it comes to arrays. I never get what I want right.

Sort does not work seamlessly. I have my key and

sort($freq);
print_r ($freq);

looks like:

Array
(
...
[1000] => 172
[1001] => 176
[1002] => 179
[1003] => 441
)

This is what I want:

Array
(
...
[and] => 172
[of] => 176
[to] => 179
[the] => 441
)



I'm beginning to think I should plug this into a MySQL table, where I 
would be at home.
But right now, I'll be stubborn and go read 
http://php.net/manual/en/array.sorting.php.

Merci Serge,
John



Re: [PHP] ksort by value

2012-09-03 Thread Serge Fonville
Hi,

Have you looked at  http://php.net/manual/en/array.sorting.php?

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table



2012/9/3 John Taylor-Johnston 

>
>  >> ...
>>> $words = preg_split('/[[:space:]]+/',$**mynewstring);
>>>
>>> foreach ($words as $word) {
>>>  $freq[$word]++;
>>> }
>>>
>>> ksort($freq);
>>> print_r ($freq);
>>> ?>
>>>
>>> ksort($freq) sorts the array by the key. And that works fine.
>>> But I would also like to sort by value to see which words are more
>>> frequent.
>>> There is no |ascending/descending option to ksort?|
>>>
>> ksort sorts by key, if you want by value, look at sort.
>>
>> As to asc/desc sort, they just have a different name. ksort and sort
>> are asc, krsort and rsort are desc equivalents.
>> - Matijn
>>
> I'm fuzzy when it comes to arrays. I never get what I want right.
>
> Sort does not work seamlessly. I have my key and
>
> sort($freq);
> print_r ($freq);
>
> looks like:
>
> Array
> (
> ...
> [1000] => 172
> [1001] => 176
> [1002] => 179
> [1003] => 441
> )
>
> This is what I want:
>
> Array
> (
> ...
> [and] => 172
> [of] => 176
> [to] => 179
> [the] => 441
>
> )
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston





ksort($freq) sorts the array by the key. And that works fine.
But I would also like to sort by value to see which words are more frequent.
There is no |ascending/descending option to ksort?|

ksort sorts by key, if you want by value, look at sort.
As to asc/desc sort, they just have a different name. ksort and sort
are asc, krsort and rsort are desc equivalents.
- Matijn

I'm fuzzy when it comes to arrays. I never get what I want right.

Sort does not work seamlessly. I have my key and

sort($freq);
print_r ($freq);

looks like:

Array
(
...
[1000] => 172
[1001] => 176
[1002] => 179
[1003] => 441
)

This is what I want:

Array
(
...
[and] => 172
[of] => 176
[to] => 179
[the] => 441
)

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



Re: [PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston





ksort($freq) sorts the array by the key. And that works fine.
But I would also like to sort by value to see which words are more frequent.
There is no |ascending/descending option to ksort?|

ksort sorts by key, if you want by value, look at sort.
As to asc/desc sort, they just have a different name. ksort and sort
are asc, krsort and rsort are desc equivalents.
- Matijn

Sort does not work seamlessly.


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



Re: [PHP] ksort by value

2012-09-03 Thread Matijn Woudt
On Mon, Sep 3, 2012 at 8:33 PM, John Taylor-Johnston
 wrote:
> Hi,
>
>  ...
> $words = preg_split('/[[:space:]]+/',$mynewstring);
>
> foreach ($words as $word) {
> $freq[$word]++;
> }
>
> ksort($freq);
> print_r ($freq);
> ?>
>
> If I have my terminology right, ksort($freq) sorts the array by the key. And
> that works fine.
> But I would also like to sort by value to see which words are more frequent.
> There is no |ascending/descending option to ksort?|
>
> Thanks,
> John

ksort sorts by key, if you want by value, look at sort.
As to asc/desc sort, they just have a different name. ksort and sort
are asc, krsort and rsort are desc equivalents.

- Matijn

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



[PHP] ksort by value

2012-09-03 Thread John Taylor-Johnston

Hi,



If I have my terminology right, ksort($freq) sorts the array by the key. 
And that works fine.

But I would also like to sort by value to see which words are more frequent.
There is no |ascending/descending option to ksort?|

Thanks,
John

Array
(
[] => 1
[a] => 165
[about] => 4
[academy] => 4
[accident] => 3
[accidental] => 1
[accused] => 1
[achieve] => 1
[across] => 1
[act] => 3
[addition] => 1
[address] => 1
[adult] => 3
[advise] => 2
[advised] => 1
[advising] => 2
[advisory] => 4
[after] => 2
[aged] => 1
[aggravated] => 1
[airport] => 4
[alarm] => 2
[alcohol] => 5
[alert] => 1
[all] => 2
[allegedly] => 2
[allow] => 1
[along] => 2
[also] => 14
[altercation] => 2
[am] => 16




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