YouTip LogoYouTip

Func Array Natcasesort

```html PHP natcasesort() Function body { font-family: Arial, sans-serif; margin: 0; padding: 0; } .container { max-width: 1200px; margin: 0 auto; padding: 20px; } pre { background-color: #f4f4f4; padding: 10px; overflow-x: auto; } code { font-family: Consolas, monospace; } .breadcrumb { background-color: #f5f5f5; padding: 10px 20px; margin-bottom: 20px; }

PHP natcasesort() Function

PHP natcasesort() Function

PHP Array Reference

Definition and Usage

The natcasesort() function sorts an array using a "natural order" algorithm. The difference between the natcasesort() function and the natsort() function is that this function is case-insensitive.

This function returns TRUE on success, or FALSE on failure.

Syntax

natcasesort(array)

Parameter Values

Parameter Description
array Required. Specifies the array to sort

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 4+

More Examples

Example 1:

Sort the elements of the $files array naturally and case-insensitively:

<?php
$files = array("txt12.txt", "Txt10.txt", "txt2.txt", "txt20.txt");
natcasesort($files);
print_r($files);
?>

Output:

Array
(
     => txt12.txt
     => txt2.txt
     => txt20.txt
     => Txt10.txt
)

Example 2

Demonstrate the difference between natsort() and natcasesort():

<?php
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");

natcasesort($array1);
echo "Case-insensitive natural sorting: ";
print_r($array1);

natsort($array2);
echo "Case-sensitive natural sorting: ";
print_r($array2);
?>

Output:

Case-insensitive natural sorting: Array
(
     => img1.png
     => img2.png
     => img10.png
     => img12.png
)
Case-sensitive natural sorting: Array
(
     => img1.png
     => img2.png
     => img10.png
     => img12.png
)
```
← Func Array NatsortFunc Array List β†’