Showing posts with label PHP Sorting Array. Show all posts
Showing posts with label PHP Sorting Array. Show all posts

Thursday, 30 August 2018

PHP sort array with date as key with date format


This question already has an answer here:


  • How to sort a date array in PHP 4 answers
I have an array as follow: The first key element is a date with the format dd-mm-yyyy
Array
(
    [08-12-2015] => Array
        (
          ------------
        )
    [07-12-2015] => Array
        (
          ------------
        )
    [09-12-2015] => Array
        (
          ------------
        )
)

Is it possible to sort this array on the first key value bij date? So the first element is
Array
(
    [07-12-2015] => Array
        (
          ------------
        )
    [08-12-2015] => Array
        (
          ------------
        )
    [09-12-2015] => Array
        (
          ------------
        )
)


Yes, I found the solution:
function order_date($a1,$b1) {
    $format = 'd-m-Y';
    $a = strtotime(date_format(DateTime::createFromFormat($format, $a1), 'Y-m-d H:i:s'));
    $b = strtotime(date_format(DateTime::createFromFormat($format, $b1), 'Y-m-d H:i:s'));
    if ($a == $b)
    {
        return 0;
    }
    else if ($a > $b)
    {
        return 1;
    }
    else {
        return -1;
    }
}

uksort($array, "order_date");

PHP Sort Array By SubArray Value by 3rd level but with clues


I'm totally desperate.


How can I sort an array in 3rd level that has an ID in the second level?
I want to sort this array only for "cars" and then ascending by "age"
array (
    ['cars'] => array (
        [23] => array(
            ['age'] => 10,
            ['color'] => 'yellow',
        ),
        [16] => array(
            ['age'] => 12,
            ['color'] => 'purple'
        ),
        [45] => array(
            ['age'] => 3,
            ['color'] => 'silver'
        )
    ),
    ['pages'] => array (
        [0] => array (
            ['number'] => 2
        )
    )
)


You can use uasort.
uasort($a['cars'], function($a, $b) {
   return $a['age'] > $b['age'] ? 1 : -1;
});


PHP sort array by the number of elements?


I have a multi-dimension array of australian states, and inside each state there are items. I was wondering if it were possible to reorder an array so the states with the most amount of items are displayed first?


Here's my array, any help would be appreciated :)
[ACT] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 14
                    [name] => Burning Log Megastore
                    [state] => ACT
                    [classid] => 1
                )

            [1] => stdClass Object
                (
                    [id] => 21
                    [name] => Fyshwick Home & Heating
                    [state] => ACT
                    [classid] => 1
                )

        )

    [NSW] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 36
                    [name] => A1 Hot Wood
                    [state] => NSW
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 40
                    [name] => Abbey Fireplaces
                    [state] => NSW
                    [classid] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 55
                    [name] => Almighty Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [3] => stdClass Object
                (
                    [id] => 70
                    [name] => Aussie Tree Services
                    [state] => NSW
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 75
                    [name] => B C Sands
                    [state] => NSW
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 76
                    [name] => B C Sands
                    [state] => NSW
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 77
                    [name] => B C Sands Mascot
                    [state] => NSW
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 113
                    [name] => Barbeques Galore (Maitland)
                    [state] => NSW
                    [classid] => 1
                )

            [8] => stdClass Object
                (
                    [id] => 114
                    [name] => Barbeques Galore Batemans Bay
                    [state] => NSW
                    [classid] => 1
                )

            [9] => stdClass Object
                (
                    [id] => 116
                    [name] => Barbeques Galore Wagga Wagga
                    [state] => NSW
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 131
                    [name] => Best Burning Fire Fuels
                    [state] => NSW
                    [classid] => 11
                )

            [11] => stdClass Object
                (
                    [id] => 132
                    [name] => Betta Burn Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [12] => stdClass Object
                (
                    [id] => 136
                    [name] => Black Forest Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [13] => stdClass Object
                (
                    [id] => 181
                    [name] => Central Coast Brick Supplies Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [14] => stdClass Object
                (
                    [id] => 188
                    [name] => Cheminee Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [15] => stdClass Object
                (
                    [id] => 249
                    [name] => Embers Heating & Restoration Supplies
                    [state] => NSW
                    [classid] => 1
                )

            [16] => stdClass Object
                (
                    [id] => 250
                    [name] => Emmbers Firewood Supplies
                    [state] => NSW
                    [classid] => 11
                )

            [17] => stdClass Object
                (
                    [id] => 292
                    [name] => Goulburn Sand & Soil
                    [state] => NSW
                    [classid] => 11
                )

            [18] => stdClass Object
                (
                    [id] => 304
                    [name] => Gunida Gunyah Aboriginal Corporation
                    [state] => NSW
                    [classid] => 11
                )

            [19] => stdClass Object
                (
                    [id] => 334
                    [name] => J  Perram Firewood Supplies
                    [state] => NSW
                    [classid] => 11
                )

            [20] => stdClass Object
                (
                    [id] => 346
                    [name] => Jetmaster (Aust) Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [21] => stdClass Object
                (
                    [id] => 402
                    [name] => Mark H Newton
                    [state] => NSW
                    [classid] => 4
                )

            [22] => stdClass Object
                (
                    [id] => 416
                    [name] => Might Burn Red Gum Pty Ltd
                    [state] => NSW
                    [classid] => 11
                )

            [23] => stdClass Object
                (
                    [id] => 430
                    [name] => Mr Stoves Pool World
                    [state] => NSW
                    [classid] => 1
                )

            [24] => stdClass Object
                (
                    [id] => 455
                    [name] => O'Brien's Redgum Sawmills
                    [state] => NSW
                    [classid] => 11
                )

            [25] => stdClass Object
                (
                    [id] => 477
                    [name] => PJ & CM Ducat
                    [state] => NSW
                    [classid] => 11
                )

            [26] => stdClass Object
                (
                    [id] => 483
                    [name] => Quality Firewood All Seasons
                    [state] => NSW
                    [classid] => 11
                )

            [27] => stdClass Object
                (
                    [id] => 487
                    [name] => R J & M E Grealy
                    [state] => NSW
                    [classid] => 11
                )

            [28] => stdClass Object
                (
                    [id] => 494
                    [name] => RN & CA Taylor
                    [state] => NSW
                    [classid] => 11
                )

            [29] => stdClass Object
                (
                    [id] => 500
                    [name] => Rouse Hill Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [30] => stdClass Object
                (
                    [id] => 504
                    [name] => Sapphire Coast Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [31] => stdClass Object
                (
                    [id] => 534
                    [name] => Sydney Heaters Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [32] => stdClass Object
                (
                    [id] => 551
                    [name] => The Woodyard
                    [state] => NSW
                    [classid] => 11
                )

            [33] => stdClass Object
                (
                    [id] => 576
                    [name] => W R Campi
                    [state] => NSW
                    [classid] => 11
                )

            [34] => stdClass Object
                (
                    [id] => 602
                    [name] => Wood 4 U
                    [state] => NSW
                    [classid] => 11
                )

            [35] => stdClass Object
                (
                    [id] => 603
                    [name] => Wood Galore
                    [state] => NSW
                    [classid] => 11
                )

            [36] => stdClass Object
                (
                    [id] => 604
                    [name] => Wood Galore
                    [state] => NSW
                    [classid] => 11
                )

        )

    [QLD] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 665
                    [name] => Brightspark Firewood Supplies
                    [state] => QLD
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 702
                    [name] => Gleno's Firewood
                    [state] => QLD
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 704
                    [name] => Gold Coast Fireplace & Barbeque Centre
                    [state] => QLD
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 732
                    [name] => Longburn Wood
                    [state] => QLD
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 742
                    [name] => Out In The Styx
                    [state] => QLD
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 746
                    [name] => Quickfire Firewood
                    [state] => QLD
                    [classid] => 11
                )

        )

    [SA] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 786
                    [name] => Adelaide Wholesale Landscape Supplies
                    [state] => SA
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 825
                    [name] => Clare Wood Yard
                    [state] => SA
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 826
                    [name] => Clewers Electrical and Furniture
                    [state] => SA
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 832
                    [name] => Cullen Transport & Firewood Supplies
                    [state] => SA
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 852
                    [name] => Enviro Systems Renewable Resources Ltd.
                    [state] => SA
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 867
                    [name] => Gawler Landscaping Supplies
                    [state] => SA
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 871
                    [name] => Green Hornet Building & Landscape Supplies
                    [state] => SA
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 899
                    [name] => Mexican Living
                    [state] => SA
                    [classid] => 1
                )

            [8] => stdClass Object
                (
                    [id] => 903
                    [name] => Mount Barker Landscape Centre
                    [state] => SA
                    [classid] => 11
                )

            [9] => stdClass Object
                (
                    [id] => 911
                    [name] => Pecan Engineering Pty Ltd
                    [state] => SA
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 919
                    [name] => PW & JD Plunkett
                    [state] => SA
                    [classid] => 11
                )

            [11] => stdClass Object
                (
                    [id] => 922
                    [name] => Renmark Firewoods & Storage Services
                    [state] => SA
                    [classid] => 11
                )

            [12] => stdClass Object
                (
                    [id] => 930
                    [name] => South Coast Firewood (FWS Inc)
                    [state] => SA
                    [classid] => 11
                )

            [13] => stdClass Object
                (
                    [id] => 937
                    [name] => Tinpak Trading Pty Ltd
                    [state] => SA
                    [classid] => 11
                )

            [14] => stdClass Object
                (
                    [id] => 940
                    [name] => Traeger's Earthmoving & Transport
                    [state] => SA
                    [classid] => 11
                )

            [15] => stdClass Object
                (
                    [id] => 947
                    [name] => Waterways Farm
                    [state] => SA
                    [classid] => 11
                )

        )

    [TAS] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 985
                    [name] => Eastern Tiers Firewood
                    [state] => TAS
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 998
                    [name] => Goods Water & Garden Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 1011
                    [name] => K-Mac Wood Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [3] => stdClass Object
                (
                    [id] => 1019
                    [name] => Leslie Vale Landscape & Gravel Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1023
                    [name] => Newmans Heating Shop
                    [state] => TAS
                    [classid] => 1
                )

            [5] => stdClass Object
                (
                    [id] => 1029
                    [name] => Pellet Fires Tasmania
                    [state] => TAS
                    [classid] => 1
                )

        )

    [VIC] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 561
                    [name] => TRT Pastoral Group
                    [state] => VIC
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 1059
                    [name] => A Grade Garden Products
                    [state] => VIC
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 1062
                    [name] => A.F. Gason Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 1088
                    [name] => Alpine Timber
                    [state] => VIC
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1093
                    [name] => Aranbe Heat
                    [state] => VIC
                    [classid] => 1
                )

            [5] => stdClass Object
                (
                    [id] => 1096
                    [name] => ASAP Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 1105
                    [name] => Bairnsdale Stoves Heaters & BBQ's
                    [state] => VIC
                    [classid] => 1
                )

            [7] => stdClass Object
                (
                    [id] => 1176
                    [name] => Burra Garden Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [8] => stdClass Object
                (
                    [id] => 1231
                    [name] => Daryl Fagan Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [9] => stdClass Object
                (
                    [id] => 1273
                    [name] => Firefox Industries Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 1297
                    [name] => Geoffrey R Gelletly
                    [state] => VIC
                    [classid] => 4
                )

            [11] => stdClass Object
                (
                    [id] => 1301
                    [name] => Gippsland Wood Heater Repairs
                    [state] => VIC
                    [classid] => 1
                )

            [12] => stdClass Object
                (
                    [id] => 1303
                    [name] => Glen Dimplex Australia Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [13] => stdClass Object
                (
                    [id] => 1306
                    [name] => Glowin' Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [14] => stdClass Object
                (
                    [id] => 1328
                    [name] => HRL Technology Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [15] => stdClass Object
                (
                    [id] => 1415
                    [name] => Moran Logging Co Pty Ltd
                    [state] => VIC
                    [classid] => 11
                )

            [16] => stdClass Object
                (
                    [id] => 1427
                    [name] => Murray Industries Kerang
                    [state] => VIC
                    [classid] => 11
                )

            [17] => stdClass Object
                (
                    [id] => 1428
                    [name] => Murray Industries Swan Hill
                    [state] => VIC
                    [classid] => 4
                )

            [18] => stdClass Object
                (
                    [id] => 1436
                    [name] => North East Fwd Strategy Implementn Comm
                    [state] => VIC
                    [classid] => 1
                )

            [19] => stdClass Object
                (
                    [id] => 1438
                    [name] => Northern Landscape Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [20] => stdClass Object
                (
                    [id] => 1448
                    [name] => Park Orchards Garden & Building Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [21] => stdClass Object
                (
                    [id] => 1476
                    [name] => Redgumdave
                    [state] => VIC
                    [classid] => 11
                )

            [22] => stdClass Object
                (
                    [id] => 1493
                    [name] => Sand, Soil & Rocks Pty Ltd
                    [state] => VIC
                    [classid] => 11
                )

            [23] => stdClass Object
                (
                    [id] => 1495
                    [name] => Shamic Sheetmetal Aust  Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [24] => stdClass Object
                (
                    [id] => 1516
                    [name] => Surrey Hills Firewood Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [25] => stdClass Object
                (
                    [id] => 1529
                    [name] => The Flue Factory Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [26] => stdClass Object
                (
                    [id] => 1553
                    [name] => Trevor King
                    [state] => VIC
                    [classid] => 11
                )

            [27] => stdClass Object
                (
                    [id] => 1585
                    [name] => Whitlands Engineering
                    [state] => VIC
                    [classid] => 1
                )

            [28] => stdClass Object
                (
                    [id] => 1591
                    [name] => Wignells of Melbourne Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [29] => stdClass Object
                (
                    [id] => 1604
                    [name] => WWF Australia
                    [state] => VIC
                    [classid] => 1
                )

            [30] => stdClass Object
                (
                    [id] => 1609
                    [name] => Yarra Timber Salvage
                    [state] => VIC
                    [classid] => 11
                )

        )

    [WA] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1623
                    [name] => All Suburbs Garden & Wood Supply
                    [state] => WA
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 1628
                    [name] => Barbecue Bazaar Cannington
                    [state] => WA
                    [classid] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 1672
                    [name] => European Wood Fired Ovens
                    [state] => WA
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 1681
                    [name] => Freo Firewood Supply
                    [state] => WA
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1740
                    [name] => Roleystone Firewood
                    [state] => WA
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 1747
                    [name] => Smart Burn Pty Ltd
                    [state] => WA
                    [classid] => 1
                )

            [6] => stdClass Object
                (
                    [id] => 1748
                    [name] => Specialised Tree Service
                    [state] => WA
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 1855
                    [name] => York Firewood & Milling
                    [state] => WA
                    [classid] => 11
                )

        )

)


After running this, $arr will now be sorted from highest populated elements to lower.
function cmpArray($a, $b)
{
    $countA = count($a);
    $countB = count($b);
    if ($countA == $countB) {
        return 0;
    }
    return ($countA < $countB) ? 1 : -1;
}

uasort($arr, "cmpArray");

PHP sort array comparing the value and pushing the smaller values ​​last


I have a value and comparing that value i want to change order of an array


for eg. here 3 is value so i need array order to be
5,6,1,2
so values less then 3 shifts last in array.
<?php

$value=3;

$array=array(6,2,5,1);

asort($array);

print_r($array);

?>


Please have a look on the below code, it may help you. You need to use array_filter with call back function to make 2 arrays then u can merge them.
  $value=3;
  $array=array(6,2,5,1);
  asort($array);
  $right = array_filter($array, function($elem) use($value){
      return $elem < $value;
  });
  $left = array_filter($array, function($elem) use($value){
     return $elem > $value;
  });
  //print_r($right);
  //print_r($left);
  $res = array_merge($left,$right);
  print_r($res);

PHP sorting table based on three internal table key values


I am looking to do some complex array sorting, but I have no idea where to start. The inner array has three relevant keys for sorting: first the year (numerical ASC), then the month (numerical ASC) and finally the name (alphabetical DESC).


<?php
// the current array:
$array = (
  array('year'=>2012, 'month'=>3, 'name'=>'John', 'score'=>12),
  array('year'=>2013, 'month'=>8, 'name'=>'Paul', 'score'=>3),
  array('year'=>2013, 'month'=>5, 'name'=>'Dennis', 'score'=>7),
  array('year'=>2012, 'month'=>3, 'name'=>'Paul', 'score'=>5),
  array('year'=>2012, 'month'=>12, 'name'=>'Paul', 'score'=>9),
  array('year'=>2012, 'month'=>9, 'name'=>'Mitt', 'score'=>3)
);

// I want to do some sorting with this as output:
$array = (
  array('year'=>2012, 'month'=>3, 'name'=>'John', 'score'=>12),
  array('year'=>2012, 'month'=>3, 'name'=>'Paul', 'score'=>5),
  array('year'=>2012, 'month'=>9, 'name'=>'Mitt', 'score'=>3),
  array('year'=>2012, 'month'=>12, 'name'=>'Paul', 'score'=>9),
  array('year'=>2013, 'month'=>5, 'name'=>'Dennis', 'score'=>7),
  array('year'=>2013, 'month'=>8, 'name'=>'Paul', 'score'=>3)
);
?>

If anyone can point me in the right direction that is really appreciated ;-).

This should work...
<?php
    // Obtain a list of columns
    // PHP 5 >= 5.5.0
    $years  = array_column($array, 'year');
    $months = array_column($array, 'month');
    $names  = array_map('strtolower', array_column($array, 'name')); // because it's a string sort.

    // Sort the data with volume descending, edition ascending
    // Add $data as the last parameter, to sort by the common key
    array_multisort($years, SORT_ASC, $months, SORT_ASC, $names, SORT_DESC, $array);
?>

EDIT : for PHP < 5.5
<?php
    // Obtain a list of columns
    // PHP < 5.5.0
    foreach ($array as $key => $row) {
        $years[$key]  = $row['year'];
        $months[$key] = $row['month'];
        $names[$key]  = strtolower($row['name']); // because it's a string sort.
    }
?>

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

sort in PHP

PHP sort() function is used to sort an array by values.
Function returns TRUE on success, or FALSE on failure.

Syntax:

sort(array,sorttype)
array : Required. Specifies the array to sort
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" );
sort($daysarr);
print_r($daysarr);
?>

O/P:

Array (
        [0] => Monday 
        [1] => Sunday
        [2] => Tuesday
       )