This should do it:
$storeArray = array();
foreach ($data['stores_cat_data'] as $store) {
$title = $store['title'];
$first_char = substr($title, 0, 1);
if (preg_match("/^[0-9]$/", $first_char)) {
$key = "0-9";
} else {
$key = strtoupper($first_char);
}
if (!isset($storeArray[$key])) {
$storeArray[$key] = array();
}
$storeArray[$key][$title] = $store;
}
var_dump($storeArray);
Your code looks almost correct. The way you determine $curr
is a little strange and you have to account for uppercase/lowercase. Also, to avoid array warnings make sure you initialize each letter of the alphabet, as you go.
You might also want to remove all non-alphanumeric characters, like "
and -
that might mess up the detection. A title of "Best" book shop
will be put under 0-9
where B
would be better.
A regexp like: preg_replace("/[^a-zA-Z0-9]/", "", $str)
should do it.