Showing posts with label PHP ksort. Show all posts
Showing posts with label PHP ksort. Show all posts

Thursday, 30 August 2018

PHP sorting table based on keys in alphabetical and numerical order then special characters


I have an array like below,


array(
   [!] = array(),
   [a] = array(),
   [.] = array(),
   [n] = array(),
   [3] = array(),
   [1] = array());

I need to sort this array as,
array(
   [a] = array(),
   [n] = array(),
   [1] = array(),
   [3] = array(),
   [!] = array(),
   [.] = array());

How to do this in php(), i refer lot of questions in stackoverflow, nothing matches my problem.so i posted this as a new question.

TL;TR

The shortest way (both in terms of code and, after some basic comparisons) is the code I suggest near the "Demo2" link below:
uksort($arr, function($a, $b)
{
    if (ctype_alnum("$a") === ctype_alnum("$b"))
        return $a > $b;
    return "$a" < "$b";
});


You can sort an array using the keys with the ksort function:
ksort($array, SORT_STRING);

The second argument is telling PHP to compare all the keys (including the numeric indexes) as strings (meaning an index like 3 will be treated as if it were '3'). This will give you an array where the keys are in ascending order. The order of the keys you show (all single characters) will be the same as their respective ASCII values (. is 46, a is 97 and so on). Seeing as you want to reverse the order, you'll have to use the array_reverse function:
$array = array_reverse($array, true);

Again, the second argument is telling PHP to preserve the keys, if not, the indexes will be reset, leaving you with a numerically indexed array.
You can skip the array_reverse call quite easily by using the uksortfunction. It works much the same way as ksort, but takes a callback function as a second argument, so you can sort by index, in descending order, too:
uksort($arr, function($a, $b)
{
    return "$a" < "$b";//added quotes to convert to strings
});

Demo

You will note that this places the '.' key in front of the '!' key. In fact, if both keys are of the same type (numeric, alpha), it would appear you want to sort them in ascending order, which you can do easily:
uksort($arr, function($a, $b)
{
    if (ctype_alnum("$a") === ctype_alnum("$b"))//both are alnums, sort ascending
        return $a > $b;//sort ascending
    return "$a" < "$b";//else sort descending
});

Demo2

If your PHP version doesn't support anonymous functions, you can define a function and pass its name as second argument, but really: you ought to upgrade:
function sortDesc($a, $b)
{
    if (ctype_alnum("$a") === ctype_alnum("$b"))
        return $a > $b;
    return (string) $a < "$b";//casting is valid, too... both do the same thing
}
uksort($array, 'sortDesc');

Thursday, 25 September 2014

ksort in PHP

PHP ksort() function is utilized to sort an array by key. The values keep their unique keys.

PHP ksort() function returns TRUE for success, or FALSE on failure.

Syntax:

ksort(array,sorttype)
 
Parameters Description: 
array : Required. Specifies the array to use.
sorttype : Optional. Specifies how to sort the array values.
   Possible values:
  • SORT_REGULAR - Default. Treat values as they are (don't change types)
  • SORT_NUMERIC - Treat values numerically
  • SORT_STRING - Treat values as strings
  • SORT_LOCALE_STRING - Treat values as strings, based on local settings

Example:

<?php
$daysarr = array( "a"=>"Sunday","b"=>"Monday","c"=>"Tuesday" );
ksort($daysarr);
print_r($daysarr);
?>

Output will be:

Array (
         [a] => Sunday
         [b] => Monday
         [c] => Tuesday
        )