Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数字组合成一组_Php - Fatal编程技术网

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;
}