计算foreach循环并在php中创建新元素

我正在为 WooCommerce 产品类别开发超级菜单。我可以使用下面的代码获取所有子类别的列表;


$parent_id = 37; //ID of the Parent Category

$subCat_of_parent = get_terms('product_cat',array('child_of' => $parent_id));

然后在如下的html结构中使用;


<div class="row">

    <div class="col-md-6">

        <ul>

        <?php

        foreach ($subCat_of_parent as $subcat) {

            ?>

            <li>

                <a href="<?php echo get_category_link($subcat->term_id); ?>"><?php echo $subcat->name; ?></a>

            </li>

            <?php

        }

         ?>

        </ul>

    </div>

</div>

这是通过以这种格式获取父类别的所有子类别列表而起作用的;


<div class="row">

    <div class="col-md-6">

        <ul>

           <li><a>1st subcategory</a></li>

           <li><a>2nd subcategory</a></li>

           <li><a>3rd subcategory</a></li>

           <li><a>4th subcategory</a></li>

           <li><a>5th subcategory</a></li>

           <li><a>6th subcategory</a></li>

        </ul>

    <div>

</div>

我想要实现的是在第三个子类别之后,它应该中断并在新列上继续,这样我就可以得到这样的东西;


<div class="row">

    <div class="col-md-6">

        <ul>

           <li><a>1st subcategory</a></li>

           <li><a>2nd subcategory</a></li>

           <li><a>3rd subcategory</a></li>

        </ul>

    <div>


    <div class="col-md-6">

        <ul>

           <li><a>4th subcategory</a></li>

           <li><a>5th subcategory</a></li>

           <li><a>6th subcategory</a></li>

        </ul>

    <div>

</div>

我怎样才能做到这一点?提前感谢您的帮助


眼眸繁星
浏览 171回答 3
3回答

收到一只叮咚

您可以使用array_chunk()将数组分成 3 组。<div class="row"><?php&nbsp; &nbsp; $chunks = array_chunk($subCat_of_parent, 3);&nbsp; &nbsp; foreach ($chunks as $group) {&nbsp; &nbsp; &nbsp; &nbsp; print '<div class="col-md-6">';&nbsp; &nbsp; &nbsp; &nbsp; print '<ul>';&nbsp; &nbsp; &nbsp; &nbsp; foreach ($group as $subcat) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print '<li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //to-do&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print '</li>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; print '</ul>';&nbsp; &nbsp; &nbsp; &nbsp; print '</div>';&nbsp; &nbsp; }?></div>

喵喵时光机

您需要每三个类别发出内部<div class="col-md-6"><ul>和</ul></div>部分。这是伪代码:发射&nbsp;<div class="row">设置一个计数器,比如&nbsp;$i = 0开始你的 for 循环,&nbsp;foreach ($subCat_of_parent as $subcat) {现在说if ($i == 0),发出开始div标签和开始ul标签。发出您的列表项$i = ($i + 1) % 3现在说if ($i == 0),发出结束ul标记和结束div标记。如果子类别的数量不是三的倍数,则最后需要额外的逻辑以确保最后一组正确关闭。

翻阅古今

<?php$arr = array(1, 2, 3, 4,5,6);&nbsp; &nbsp; &nbsp; &nbsp; $count = 0;?>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; foreach ($subCat_of_parent as $subcat)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // echo "count =".$count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($count%3 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo ('<div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count++;&nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php echo get_category_link($subcat->term_id); ?>"><?php echo $subcat->name; ?></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($count%3 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo ('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul></div>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP