Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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:将每组以相同字母开头的单词包装在一个div中_Php_Loops - Fatal编程技术网

PHP:将每组以相同字母开头的单词包装在一个div中

PHP:将每组以相同字母开头的单词包装在一个div中,php,loops,Php,Loops,我有一个循环,它按字母顺序对一组结果进行排序,并用当前字母显示一个,但我找不到一种方法将每个子集包装在一个div中 <?php $previousLetter = null; foreach($allBrands as $brand) { $firstLetter = strtolower($brand->name[0]); if ( $previousLetter != $firstLetter ) { echo '<span cl

我有一个循环,它按字母顺序对一组结果进行排序,并用当前字母显示一个
,但我找不到一种方法将每个子集包装在一个div中

<?php

$previousLetter = null;

foreach($allBrands as $brand) {


    $firstLetter = strtolower($brand->name[0]);

    if ( $previousLetter != $firstLetter ) {

        echo '<span class="designer-first-letter">'. $firstLetter .'</span>';

        $previousLetter = $firstLetter;


    }

    echo '<p>'.$brand->name.'</p>';

}

在循环之前,如何对品牌进行预分组

$brands = Array("Aword", "Aword2", "Aword3", "BWord", "Bword2");
$groups = Array();

foreach($brands as $brand) { 
    $startsWith = strtolower($brand[0]);

    if( array_key_exists($startsWith, $groups))
        array_push($groups[$startsWith], $brand);
    else 
    {
        $groups[$startsWith] = Array($brand);
    }
}

ksort($groups);

foreach($groups as $key => $value ) {
    ?>
    <div>
        <span><?php echo strtoupper($key) ?></span>
        <?php foreach($value as $brand) { ?>
        <p><?php echo $brand?></p>
        <?php } ?>
    </div>
    <?php
}
$brands=数组(“Aword”、“Aword2”、“Aword3”、“BWord”、“Bword2”);
$groups=Array();
foreach($brands作为$brand){
$startsWith=strtolower($brand[0]);
如果(数组\键\存在($startsWith$groups))
阵列推送($groups[$startsWith],$brand);
其他的
{
$groups[$startsWith]=阵列($brand);
}
}
ksort($组);
foreach($key=>$value的组){
?>


非常感谢,非常有效。它们已经按字母顺序排序了,因为它们来自数据库。我个人会先设置底层结构,然后再将
$brands
的内容排序到其中,而不是在通过
$brands
时进行设置。除此之外,这种方法比尝试在每个循环的
$brands
中打印
,因为此代码可读性更好。
<span>A</span>
<p>Aword</p>
<p>Aword2</p>
<p>Aword3</p>
<p>...</p>

<span>B</span>
<p>Bword</p>
<p>Bword2</p>
<p>Bword3</p>
<p>...</p>

<span>C</span>
<p>Cword</p>
<p>Cword2</p>
<p>Cword3</p>
<p>...</p>

...
$brands = Array("Aword", "Aword2", "Aword3", "BWord", "Bword2");
$groups = Array();

foreach($brands as $brand) { 
    $startsWith = strtolower($brand[0]);

    if( array_key_exists($startsWith, $groups))
        array_push($groups[$startsWith], $brand);
    else 
    {
        $groups[$startsWith] = Array($brand);
    }
}

ksort($groups);

foreach($groups as $key => $value ) {
    ?>
    <div>
        <span><?php echo strtoupper($key) ?></span>
        <?php foreach($value as $brand) { ?>
        <p><?php echo $brand?></p>
        <?php } ?>
    </div>
    <?php
}