Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

natsort()

Example

Sort an array using a natural order algorithm, keeping the original keys.

<?php
$temp_files = array(“temp15.txt”,“temp10.txt”,
“temp1.txt”,“temp22.txt”,“temp2.txt”);

sort($temp_files);
echo “Standard sorting: “;
print_r($temp_files);
echo “<br>”;

natsort($temp_files);
echo “Natural order: “;
print_r($temp_files);
?>

Definition and Usage

The natsort() function sorts an array using a “natural order” algorithm, which treats numbers in a human-friendly manner.

For example, in natural order sorting, 2 is considered less than 10, whereas in computer-based sorting, 10 would be considered less than 2. The function retains the original array k

Syntax

natsort(array)

Parameter Values

 

Parameter

Description

array

Required. Specifies the array to be sorted.

Technical Details

Return Value:

Returns TRUE on success or FALSE on failure.

PHP Version:

4+

PHP Changelog:

PHP 5.2.1: Zero-padded numeric strings (e.g., ‘00006’) now ignore the leading zeros.