xProteuSx Posted February 18, 2014 Share Posted February 18, 2014 So I've got a list of members, that looks something like this: aardvark CandyFISH Anthony55 Beezl3bub angie13 007_bond I am trying to sort these members alphabetically, but case insensitively as well. My sorting function looks like this, and sorts an array of arrays based on the first character of the second field of each sub-array: function sortmulti($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) { if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key) {$temp[$key]=$array[$key][$index];} if(!$natsort) { if ($order=='asc') {asort($temp);} else {arsort($temp);} } else { if ($case_sensitive===true) {natsort($temp);} else {natcasesort($temp);} if($order!='asc') {$temp=array_reverse($temp,TRUE);} } foreach(array_keys($temp) as $key) { if (is_numeric($key)) {$sorted[]=$array[$key];} else {$sorted[$key]=$array[$key];} } return $sorted; } return $sorted; } Then I call the function up like this: $users_array = sortmulti($users_array, 1, 'asc'); In the end I end up with sorting that looks like this: 007_bond Anthony55 Beezl3bub CandyFISH aardvark angie13 As you can see the sorting considers all upper case characters to be first, followed by lower case characters. I am looking for something like this: 007_bond aardvark angie13 Anthony55 Beezl3bub CandyFISH Any pointers on how I can do this? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/ Share on other sites More sharing options...
gristoi Posted February 18, 2014 Share Posted February 18, 2014 sort($array, SORT_FLAG_CASE); Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/#findComment-1469379 Share on other sites More sharing options...
xProteuSx Posted February 18, 2014 Author Share Posted February 18, 2014 I am not sure whether I am not understanding something, or whether this function does not sort multidimensional arrays. When I run this nothing happens (still an unordered mess): $users_array = sortmulti($users_array, 1, 'asc'); sort($users_array, SORT_FLAG_CASE); So then I try this, but get an error: $users_array = sortmulti($users_array, 1, 'asc'); sort($users_array[1], SORT_FLAG_CASE); //index 1 is the 'username' field in the sub-arrays Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/#findComment-1469391 Share on other sites More sharing options...
xProteuSx Posted February 18, 2014 Author Share Posted February 18, 2014 I am looking at this example: <?php// Obtain a list of columnsforeach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition'];}// Sort the data with volume descending, edition ascending// Add $data as the last parameter, to sort by the common keyarray_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);?> And also this one: <?php $array = array('Alpha', 'atomic', 'Beta', 'bank');$array_lowercase = array_map('strtolower', $array);array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);print_r($array);?> However, I don't know whether is possible to combine the two and, if it is, how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/#findComment-1469395 Share on other sites More sharing options...
gristoi Posted February 18, 2014 Share Posted February 18, 2014 sorry, didn't see the multidimensional comment: usort($arr, function($a, $b) { $a1 = $a["username"]; $b1 = $b["username"]; $out = strcasecmp($a1,$b1); if($out == 0){ return 0;} if($out > 0){ return 1;} if($out < 0){ return -1;} }); Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/#findComment-1469396 Share on other sites More sharing options...
kicken Posted February 18, 2014 Share Posted February 18, 2014 Where are you getting this list of members from? If it's from a database then just add an ORDER BY clause to your select query and call it a day. Otherwise, you'll want to use usort() as shown above. Quote Link to comment https://forums.phpfreaks.com/topic/286285-case-insensitive-ordering-of-members/#findComment-1469401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.