Sort with natural algorithm (strnatcmp) and default sorting algorithms (strcmp) in PHP

Description

The following code shows how to sort with natural algorithm (strnatcmp) and default sorting algorithms (strcmp).

Example


//from   w  w  w  .j  a  v a  2  s .c o  m
<?php
    $arr1 = $arr2 = array("pic1","pic2","pic10","pic01","pic100");
    echo "Standard string comparison"."<br>";
    usort($arr1,"strcmp");

    print_r($arr1);
    echo "<br>";

    echo "Natural order string comparison"."<br>";
    usort($arr2,"strnatcmp");

    print_r($arr2);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone