w3resource
php Programming Exercies

PHP Array Exercises : Sort entity letters

PHP Array: Exercise-25 with Solution

Write a PHP function to sort entity letters.

Sample Solution:

PHP Code:

<?php
function entity_sort($my_array) {
    $total = count($my_array);
    for ($i=0;$i<$total;$i++) {
        if ($my_array[$i]{0} == '&') {
            $my_array[$i] = $my_array[$i]{1}.$my_array[$i];
        } else {
            $my_array[$i] = $my_array[$i]{0}.$my_array[$i];
        }
    }
    sort($my_array);
        for ($i=0;$i<$total;$i++) {
        $my_array[$i] = substr($my_array[$i],1);
    }
        return $my_array;
    }
$arr = array(" ","&", "<");
print_r(entity_sort($arr));
?>

Sample Output:

Array                                                       
(                                                           
    [0] =>                                                  
    [1] => <                                                
    [2] =>                                                  
)

Flowchart:

Flowchart: PHP - Sort entity letters

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP script to sort an array using case-insensitive natural ordering.
Next: Write a PHP function to shuffle an associative array, preserving key, value pairs.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming