Answer the question
In order to leave comments, you need to log in
How to split an array of php links into blocks by the first letter of the link title?
There is an array like
rray
(
[0] => Array
(
[name] => Bryansk
)
[1] => Array
(
[name] => Voronezh
)
[2] => Array
(
[name] => Ekaterinburg
)
... [name] - these are links .
how can you make a division between cities for each letter in the output?
B
Bryansk
V
Voronezh
....
Answer the question
In order to leave comments, you need to log in
<?php
$data = [
[
'name' => 'Брянск'
],
[
'name' => 'Москва'
],
[
'name' => 'Бобруйск'
],
];
usort($data, function ($v1, $v2) {
return strcmp($v1['name'], $v2['name']);
});
$letter = null;
foreach ($data as $v) {
$currentLetter = mb_substr($v['name'], 0, 1, 'utf-8');
if ($currentLetter !== $letter) {
$letter = $currentLetter;
echo "--- $letter ---", PHP_EOL;
}
echo $v['name'], PHP_EOL;
}
It's not entirely clear what "[name] are links." means.
but generally something like that.
$cities = array(...);
$cities_with_letters = array();
foreach ($cities as $city) {
$letter = substr($city['name'], 0, 1);
$cities_with_letters[$letter][] = $city;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question