Anonymous Anonymous - 1 month ago
379 0

sortByLastCharacter() functie

PHP

SF test 3

<?php
    function sortByLastCharacter($inStr){
        $result = array();
        $rawArr = explode(" ", $inStr);
        // foreach($rawArr as $item){
        //     if(array_key_exists($item[-1], $result))    /*+duplicate count bij key name*/;
        //     else                                        $result[$item[-1]] = $item;
        // }
        for($i = 0; $i < sizeof($rawArr); $i++){
            if(array_key_exists($rawArr[$i][-1], $result))   $result[$rawArr[$i][-1].$i] = $rawArr[$i];
            else                                            $result[$rawArr[$i][-1]] = $rawArr[$i];
        }
        ksort($result);
        return($result);
    }

    $input = "qwertc qwertcb qwertab qwerta qwertbb";
    print("input is: ".$input)."<hr>";
    print_r(sortByLastCharacter($input));
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download