我需要显示一个包含数据库链接的页面
array (size=5)
0 =>
array (size=3)
'id' => string '1' (length=1)
'name' => string 'apple' (length=5)
'url' => string 'http://iphone7' (length=14)
1 =>
array (size=3)
'id' => string '2' (length=1)
'name' => string 'samsung' (length=7)
'url' => string 'http://samsung' (length=14)
2 =>
array (size=3)
'id' => string '3' (length=1)
'name' => string 'xiaom' (length=5)
'url' => string 'http://xiaomi' (length=13)
3 =>
array (size=3)
'id' => string '6' (length=1)
'name' => string 'sony' (length=4)
'url' => string 'http://sony' (length=11)
4 =>
array (size=3)
'id' => string '7' (length=1)
'name' => string 'nokia' (length=5)
'url' => string 'http://nokia' (length=12)
喜欢
A
apple <-- it's a link name with url
N
nokia
S
samsung
sony
X
xaomi
以字母顺序加上字母(大写) 我知道如何使用普通数组(只是名称)
$result = array();
$previous = null;
$i = 0;
foreach ($this->model as $value) {
$firstLetter = substr($value, 0, 1);
if ($previous !== $firstLetter) {
$result[$i] = mb_convert_case($firstLetter, MB_CASE_UPPER, "UTF-
8");
$i++;
}
$result[$i] = $value;
$previous = $firstLetter;
$i++;
}
但是我不明白如何使用多维数组。 我可以首先按“名称”属性进行排序,但如何获得首个字母以放入新数组
[
"A" = > array ('name' => apple,'url' => 'http://...'),
"S" => array ('name' => samsung, 'url' => 'http://...')
]
与foreach运算符一起显示
我使用此功能获取首字母
$result = array();
$previous = null;
$i = 0;
foreach ($model as $value => $key) {
$firstLetter = substr($key["name"], 0, 1);
if ($previous !== $firstLetter) {
$result[$firstLetter] = $firstLetter;
$result[$firstLetter[$i]] = $key;
}
// something must be here to assign secons array to
$result[$firstLetter
$previous = $firstLetter;
}
我得到
array (size=4)
'a' =>
array (size=3)
'id' => string '1' (length=1)
'name' => string 'apple' (length=5)
'url' => string 'http://iphone7' (length=14)
'n' =>
array (size=3)
'id' => string '7' (length=1)
'name' => string 'nokia' (length=5)
'url' => string 'http://nokia' (length=12)
's' =>
array (size=3)
'id' => string '2' (length=1)
'name' => string 'samsung' (length=7)
'url' => string 'http://samsung' (length=14)
'x' =>
array (size=3)
'id' => string '3' (length=1)
'name' => string 'xiaom' (length=5)
'url' => string 'http://xiaomi' (length=13)
答案 0 :(得分:1)
您可以使用array_multisort
array_multisort( array_column($yourArray, "name"), SORT_ASC, $yourArray );