tags:

views:

67

answers:

4

Hi all,

I got an array which has 7 types of fruit:

$fruits = array(
  "lemon", 
  "orange", 
  "banana", 
  "apple", 
  "cherry", 
  "apricot", 
  "Blueberry"
);

I don't know how to print out the data in a way that the outcome will like this:

<A>
Apple, Apricot <!--(Note that Apricot is followed by Apple in alphabetic order)-->
<B>
Banana
<C>
Cherry
<L>
Lemon
<O>
Orange

I am sorry that the question may be a bit difficult. But please kindly help if you could.

+2  A: 

I'd first use an array sorting function like sort(), and then create a loop that goes through the array and keeps track of what the first letter of the last word it output is - each time the first letter changes, output a new heading first.

Amber
+2  A: 

Try this:

sort($fruit);
$lastLetter = null;

foreach ($fruit as $f) {
    $firstLetter = substr($f, 0, 1);
    if ($firstLetter != $lastLetter) {
        echo "\n" . $firstLetter . "\n";
        $lastLetter = $firstLetter;
    }
    echo $f . ", ";
}

There's some tidying up needed from that snippet, but you get the idea.

nickf
you can use `$firstLetter = $f[0]` which avoids a function call
Tom Haigh
true, though I prefer using the string functions. given PHP's loose typing, it makes the code a bit clearer IMO.
nickf
@Tom How do you know that it doesn't do the same thing in the background anyway?
SeanJA
@SeanJA: I think that there is going to be some overhead in calling a function and passing the string to it, etc. Probably very small though.
Tom Haigh
@Tom: In the background though, php could recognize that these are the same thing and optimize it out for you (I am not saying that it is this smarty... but it could be, so the micro optimization makes little sense and makes the code less readable).
SeanJA
@SeanJA: I think unlikely as they are not exactly the same thing. substr() is a function call. I don't really want to completely hijack the answer by arguing, but I think it's pretty subjective whether `$f[0]` is less readable than `substr($f, 0, 1)`. But I regret posting the first comment now, sorry.
Tom Haigh
+2  A: 

You can do this:

// make the first char of each fruit uppercase. 
for($i=0;$i<count($fruits);$i++) {
        $fruits[$i] = ucfirst($fruits[$i]);
}

// sort alphabetically.
sort($fruits);

// now create a hash with first letter as key and full name as value.
foreach($fruits as $fruit) {
        $temp[$fruit[0]][] = $fruit;
}

// print each element in the hash.
foreach($temp as $k=>$v) {
        print "<$k>\n". implode(',',$v)."\n";
}

Working example

codaddict
You could also use `natcasesort` instead of changing the data.
nickf
+2  A: 

This should do what you are looking for:

    $fruits = array("lemon","orange","banana","apple","cherry","apricot","Blueberry");

//place each fruit in a new array based on its first character (UPPERCASE)
$alphaFruits = array();
foreach($fruits as $fruit) {
    $firstChar = ucwords(substr($fruit,0,1));
    $alphaFruits[$firstChar][] = ucwords($fruit);
}

//sort by key
ksort($alphaFruits);

//output each key followed by the fruits beginning with that letter in a order
foreach($alphaFruits as $key=>$fruits) {
    sort($fruits);
    echo "<{$key}>\n";      
    echo implode(", ", $fruits)."\n";   
}
jkilbride