• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Sorting String Arrays

Niaad

New Member
I have a feeling there's a function that does this, somewhere, but for some reason I can't seem to get PHP to sort string arrays correctly.

Don't get me wrong, it does sort them, to an extent. The problem lies in the fact that an array containing the values "Jim", "andy", and "Zend" would be sorted like this:

Jim
Zend
andy

Obviously, it's because the a on Andy is not capitalized. I realize there is a function that effectively changes all of the array's values to either all lowercase or all uppercase and that could be used in the sort() function; however, this function, array_change_key_case() does not work--I just get call to bad function errors whenever I attempt to use it (I tried this on two different hosts).

Is there any way to get PHP to sort string arrays and not pay attention to the case?
 
In recent version of php, usort($array, 'strcasecmp'); does work properly now (case insensitive), AFAICT. I'm not sure how far back the fix goes, but it worked fine on php 4.2.1.

Example code:

Code:
$test = Array(
	'Jim',
	'andy',
	'Zend'
);

usort($test, 'strcasecmp');
reset($test);

for($i = 0; $i < 3; $i++)
{
	print $test[$i] . "<BR />\n";
}
Will print "andy, Jim and Zend" in that order, on their own line.
 
Back
Top