Php 如何将数字组合成一组
下面的代码根据起始字符将字符串分成若干组。我现在尝试将所有数字合并到一个组中,而不是每个起始数字都有自己的组。有人能帮我吗,因为我不知道如何修改以实现这一点Php 如何将数字组合成一组,php,Php,下面的代码根据起始字符将字符串分成若干组。我现在尝试将所有数字合并到一个组中,而不是每个起始数字都有自己的组。有人能帮我吗,因为我不知道如何修改以实现这一点 $last = ''; foreach($brandsArray as $words){ $current = substr($words, 0, 1); if(strtoupper($current) != strtoupper($last)) { echo "\n <a name
$last = '';
foreach($brandsArray as $words){
$current = substr($words, 0, 1);
if(strtoupper($current) != strtoupper($last)) {
echo "\n
<a name=\"". strtoupper($current) ."\"><li class=\"title\">" . strtoupper($current) . "</li></a>\n\n";
}
echo '<li>'. $words . "</li>\n";
$last = $current;
}
我希望输出是什么样子的
**#**
1
121
57
876
**A**
Apple
Apple1
**B**
Banana
Banana123
**D**
Delta
它目前正在做的事情如下
**1**
1
121
**5**
57
**8**
876
**A**
Apple
Apple1
**B**
Banana
Banana123
**D**
Delta
对于上述问题中给定的输入和预期输出
$brandsArray = array('1', '121', '57', '876', 'Apple', 'Apple1', 'Banana', 'Banana123', 'Delta');
$last = '';
foreach($brandsArray as $words){
// setting the current value as # if is number
$current = is_numeric($words)?"#":substr($words, 0, 1);
if (strtoupper($current) != strtoupper($last)) {
echo "\n
<a name=\"". strtoupper($current) ."\"><li class=\"title\">" . strtoupper($current) . "</li></a>\n\n";
}
echo '<li>'. $words . "</li>\n";
$last = $current;
}
请提供输入和预期输出的示例。strothigh$words\n\n@VeshrajJoshi-更新以显示输入、它在做什么以及我希望它做什么
$brandsArray = array('1', '121', '57', '876', 'Apple', 'Apple1', 'Banana', 'Banana123', 'Delta');
$last = '';
foreach($brandsArray as $words){
// setting the current value as # if is number
$current = is_numeric($words)?"#":substr($words, 0, 1);
if (strtoupper($current) != strtoupper($last)) {
echo "\n
<a name=\"". strtoupper($current) ."\"><li class=\"title\">" . strtoupper($current) . "</li></a>\n\n";
}
echo '<li>'. $words . "</li>\n";
$last = $current;
}