PHP: Putting a string in alphabetical order -
i'm making website list books, , want artists/authors names in alphabetical order. made php, breaks when there more 2 artists/author... can me? phpfiddle link: http://phpfiddle.org/main/code/se6p-wpw3
code:
<?php $artist = "john doe, ami, lolll"; if (strpos($artist,',') !== false) { echo "multiple artists\n"; $artistchar = str_split($artist); $start = 0; $artistnum = 0; ($i = 0; $i < count($artistchar); $i = $i + 1) { //echo ($i)."\n"; //echo ($artistchar[$i])."\n"; if ($artistchar[$i] == ',') { echo "implode\n"; echo ($i)."\n"; echo ($start)."\n"; $stop = $i; echo ($stop)."\n"; $artistnum = $artistnum + 1; ${'artist'.$artistnum} = implode(array_slice($artistchar, $start, $stop)); echo (${'artist'.$artistnum})."\n"; $i = $i + 1; $start = $i + 1; } if ($i == count($artistchar)) { echo "implode2\n"; $artistnum = $artistnum + 1; ${'artist'.$artistnum} = implode(array_slice($artistchar, $start, $i)); echo (${'artist'.$artistnum})."\n"; } } } else { echo 'only 1 artist'; } ?>
$artist_aray = explode(',', $artist); // split input @ commas $artist_array = array_map('trim', $artist_array); // remove spaces around commas sort($artist_array); // put them in alphabetical order foreach ($artist_array $a) { echo "$a\n"; }
Comments
Post a Comment