PHP Array Exercises : Sort an array using case-insensitive natural ordering
PHP Array: Exercise-24 with Solution
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:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP program to sort a multi-dimensional array set by a specific key.
Next: Write a PHP function to sort entity letters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php-exercises/php-array-exercise-24.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics