Discussion:
Case insensitive ksort
(too old to reply)
Christoph Boget
2007-09-18 15:37:01 UTC
Permalink
I looked in the docs but didn't see anything regarding case
insensitivity and I fear the functionality doesn't exist. I'm just
hoping I'm looking in the wrong place.

Is there a way to get ksort to work without regard to case? When I
sort an array using ksort, all the upper case keys end up at the top
(sorted) and all the lower case keys end up at the bottom (sorted).
Ideally, I'd like to see all the keys sorted (and intermixed)
regardless of case. Am I going to have to do this manually? Or is
there an internal php command that will do it for me?

thnx,
Christoph
Jim Lucas
2007-09-18 15:45:08 UTC
Permalink
Post by Christoph Boget
I looked in the docs but didn't see anything regarding case
insensitivity and I fear the functionality doesn't exist. I'm just
hoping I'm looking in the wrong place.
Is there a way to get ksort to work without regard to case? When I
sort an array using ksort, all the upper case keys end up at the top
(sorted) and all the lower case keys end up at the bottom (sorted).
Ideally, I'd like to see all the keys sorted (and intermixed)
regardless of case. Am I going to have to do this manually? Or is
there an internal php command that will do it for me?
thnx,
Christoph
Is this what you are looking for?

<?php

$yourarray = array();

$sorted = natcasesort(array_keys($yourarray));

foreach ( $sorted AS $key ) {
echo $yourarray[$key];
}

?>
--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
Robin Vickery
2007-09-18 15:57:33 UTC
Permalink
Post by Christoph Boget
I looked in the docs but didn't see anything regarding case
insensitivity and I fear the functionality doesn't exist. I'm just
hoping I'm looking in the wrong place.
Is there a way to get ksort to work without regard to case? When I
sort an array using ksort, all the upper case keys end up at the top
(sorted) and all the lower case keys end up at the bottom (sorted).
Ideally, I'd like to see all the keys sorted (and intermixed)
regardless of case. Am I going to have to do this manually? Or is
there an internal php command that will do it for me?
uksort($array, 'strcasecmp');

-robin
TG
2007-09-18 16:36:35 UTC
Permalink
I don't have time to look up in the manual but I don't remember a case
insensitive ksort().

I think there's a uksort() where you could specify your own sorting
parameters for the keys.

Also, you could create the array with keys run through strtoupper() or
strtolower(). If you need the proper upper/lowercase version too, you
could store that separately within the array.

Just some thoughts on how you could do this. good luck!

-TG

----- Original Message -----
From: "Christoph Boget" <***@gmail.com>
To: php-***@lists.php.net
Date: Tue, 18 Sep 2007 11:37:01 -0400
Subject: [PHP] Case insensitive ksort
Post by Christoph Boget
I looked in the docs but didn't see anything regarding case
insensitivity and I fear the functionality doesn't exist. I'm just
hoping I'm looking in the wrong place.
Is there a way to get ksort to work without regard to case? When I
sort an array using ksort, all the upper case keys end up at the top
(sorted) and all the lower case keys end up at the bottom (sorted).
Ideally, I'd like to see all the keys sorted (and intermixed)
regardless of case. Am I going to have to do this manually? Or is
there an internal php command that will do it for me?
thnx,
Christoph
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...