PHP: Make an Alphabetical Selection from Elements of an Array

Here’s a snippet that extracts elements from an array starting with a specific letter (alphabetical search).

$programming_languages = array('A+', 'A++', 'A# .NET', 'A# (Axiom)', 'A-0', 'B', 'Baja', 'BeanShell', 'C#', 'Cayenne', 'JavaScript', 'Java', 'JScript', 'Python', 'Perl', 'PHP', 'Visual Basic .NET', 'Visual FoxPro', 'ZOPL', 'ZPL');

function alpha_search($array, $letter = 'a') // 'a' is the default value
return array_filter($array, create_function('$var', 'return (strtolower(trim($var{0})) == strtolower("'.$letter.'"));'));

/* Values starting with 'A' */
echo '<pre>'; print_r(alpha_search($programming_languages, 'a'));  echo '</pre>'; 

/* Values starting with 'V' */
echo '<pre>'; print_r(alpha_search($programming_languages, 'v'));  echo '</pre>';

NOTE: In the second argument you can use either a lower case letter or an upper case letter. It’s case insensitive.

Here’s how our sample ouput will look like:

    [0] => A+
    [1] => A++
    [2] => A# .NET
    [3] => A# (Axiom)
    [4] => A-0

    [16] => Visual Basic .NET
    [17] => Visual FoxPro

PHP: Usage of Range() Function (Alphabetical & Numerical Output)


In this tutorial you will see some usage examples of the function range(). They are very effective when you need to create a range of elements. For instance let’s say you need to list some content using an alphabetical search. Here’s a way to do it:

Alternative array


$non_alpha = array('#');
$alphabetical_array = range('a','z'); 

$final_array = array_merge($non_alpha, $alphabetical_array);

function AddLinks($letter)
$title = strtoupper($letter);
$a = "<a href='alpha_search.php?letter=".$letter."'>".$title."</a>";

// Use array_map to modify the array by adding the links

$format_array = array_map("AddLinks", $final_array);

echo implode(" | ", $format_array);

Our code will nicely output the alphabetical navigation links with the URL for each letter:


Other examples of range() usage:

// 1, 2, 3, 4, 5, 6 ... 30

$sample_one = range(1,30);

echo implode(", ", $sample_one)."<br />";

Start with 50, up to 200 (increment by 10)
50, 60, 70, 80, 90 ... 200

$sample_two = range(50, 200, 10); 

echo implode(", ", $sample_two)."<br />";

// 20, 19, 18, 17, 16 .. 3, 2, 1
$sample_three = range(20, 1); 

echo implode(", ", $sample_three)."<br />";

Note: This range() function returns elements from low to high, but if low > high then the sequence will be from high to low.

PS: This article is translated to Serbo-Croatian language by WHG Team.

How to make an alphabetical search

This tutorial is aimed to help you how to make an alphabetical search using MySQL. Many sites use it these day and we will show you how do they do it.

Let’s suppose you have a business directory and you want to display the listings that are starting with a specific letter or a non-letter character (numeric, etc).

Let’s start with the non-alphabetic selection in case you need to select listings starting with a number or a non-alphanumeric character. Here’s the command:

SELECT * FROM `listings` WHERE Left(listing_name, 1) REGEXP '^[^a-z]+$';

The regular expressions are used here. The MySQL command selects all listings that have a first non-alphabetical character.

Now let’s move on with the alphabetical selection.

We will choose the letter ‘A’ for our example. Here’s the command that will select all listings starting with ‘A’.

SELECT * FROM `listings` WHERE Left(listing_name, 1) = 'a'; -- case insensitive

You can replace ‘A’ with any letter you wish.

Congratulations! Now you know how to perform an alphabetical search with MySQL.