Separating a list of entries by the first letter in a given field
I'm trying to list a series of entries by the first letter of the entry title.
How would I do this, and b, separate each letter per each of the Alphabet.
Thanks
Let's say I have two titles:
Much Ado About Nothing
The Tempest
I'd need to separate the titles according to the first letter:
//
K
L
M - Much Ado About Nothing
N
//
S
T The Tempest
U
V
etc.
EDIT: This is the array I have, and I need to select the title from it:
Array
(
[0] => Array
(
[id] 开发者_StackOverflow中文版=> 1
[title] => Test Article
[author] => Shamil
[date] => 1309039548
[summary] => Summary of test article
[content] =>
Technical article Content.
[category] => technical
[published] => 0
)
)
Assuming all these entries comes in an array, and the titles are capitalized, you can do the following:
$entries = array(...); // the original entries
sort($entries);
$alphabetized = array();
foreach (range('A', 'Z') as $letter) {
$alphabetized[$letter] = array();
}
foreach ($entries as $entry) {
array_unshift($alphabetized[$entry[0]], $entry);
}
The only problem with this algorithm is that it will alphabetize something like "The Matrix" under T instead of M. But at least this is a start.
Update:
Here's code I made to handle "The" and "A":
<?php
$entries = array(
array('title' => 'Much Ado About Nothing'),
array('title' => 'Star Wars'),
array('title' => 'The Tempest'),
array('title' => 'A Wrinkle In Time'),
array('title' => 'Star Trek'),
array('title' => 'The'),
array('title' => 'A')
);
function shamil_title_compare($a, $b) {
return strcasecmp($a['title'], $b['title']);
}
usort($entries, 'shamil_title_compare');
$alphabetized = array();
foreach (range('A', 'Z') as $letter) {
$alphabetized[$letter] = array();
}
foreach ($entries as $entry) {
$title = $entry['title'];
$firstWord = strtok($title, ' ');
if (!in_array($firstWord, array('The', 'A'))) {
$alphabetized[$firstWord[0]][] = $entry;
} else {
$nextWord = strtok(' ');
if ($nextWord !== false) {
$alphabetized[$nextWord[0]][] = $entry;
} else {
$alphabetized[$firstWord[0]][] = $entry;
}
}
}
Update 2 I've updated my code to output the alphabetized list into an html unordered list:
<?php
$entries = array(
array('title' => 'Much Ado About Nothing'),
array('title' => 'Star Wars'),
array('title' => 'A Quantum of Solace'),
array('title' => 'The Tempest'),
array('title' => 'Cat Attack'),
array('title' => 'A Wrinkle In Time'),
array('title' => 'Star Trek'),
array('title' => 'The Cat'),
array('title' => 'Wicked Witch of the West'),
);
// Build a copy of the original, but strip "The" and "A", and lowercase it.
$sort = array();
foreach ($entries as $index => $array)
{
$title = strtoupper($array['title']);
list($head, $tail) = explode(' ', $title, 2);
if (($head !== 'A') and ($head !== 'THE'))
{
$sort[] = $title;
}
else if ($tail != '')
{
$sort[] = $tail;
}
else
{
$sort[] = $head;
}
}
array_multisort($sort, SORT_ASC, SORT_STRING, $entries);
$alphabetized = array();
foreach (range('A', 'Z') as $letter)
{
$alphabetized[$letter] = array();
}
foreach ($sort as $index => $title)
{
$letter = $title[0];
$alphabetized[$letter][] = $entries[$index];
}
// Output as html
foreach ($alphabetized as $letter => $entries)
{
echo "<h3>{$letter}</h3>", PHP_EOL,
"<ul>", PHP_EOL;
foreach ($entries as $entry)
{
echo "<li>{$entry['title']}</li>", PHP_EOL;
}
echo "</ul>", PHP_EOL;
}
精彩评论