PHP Array Exercises : Sort an array using case-insensitive natural ordering
24. Case-Insensitive Natural Order Sorting
Write a PHP script to sort an array using case-insensitive natural ordering.
Sample Solution:
PHP Code:
<?php
// Define an array of colors with non-sequential keys
$colors = array(
"color1", "color20", "color3", "color2"
);
// Sort the array in natural order, case-insensitive
sort($colors, SORT_NATURAL | SORT_FLAG_CASE);
// Iterate through the sorted array and display each element
foreach ($colors as $key => $val) {
echo "Colors[" . $key . "] = " . $val . "\n";
}
?>
Output:
Colors[0] = color1 Colors[1] = color2 Colors[2] = color3 Colors[3] = color20
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to sort an array of mixed-case strings using natural order sorting in a case-insensitive manner.
- Write a PHP function to apply natsort() with case insensitivity to sort an array and then output the sorted values.
- Write a PHP program to compare case-insensitive natural sorting with standard sorting to highlight differences in ordering.
- Write a PHP script to sort an array of filenames naturally while ignoring case sensitivity, then display them in a formatted list.
Go to:
PREV : Sort Multi-Dimensional Array by Specific Key.
NEXT : Sort Entity Letters.
PHP Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.