faq
faq

Reputation: 3076

Sorting a array in alphabetical order in php?

<?php
$string = "qwertyuiopasdfghjklzxcvbnm";
$string = explode("", $string);
sort($string);
foreach ($string as $val) {
    echo $val."<br>";
}
?>

I want this to output: a b c ... but how?

Upvotes: 0

Views: 1404

Answers (1)

user149341
user149341

Reputation:

Your current call to explode() isn't working -- it doesn't accept an empty first argument. Try using str_split() instead:

$string = "qwertyuiopasdfghjklzxcvbnm";
$array = str_split($string, 1);
sort($array);
foreach ($array as $val) {
    echo $val."<br>";
}

Upvotes: 6

Related Questions