Looping with php in an HTML class

0

Let's suppose I have a div inside a foreach and inside that div I have another div with a class x, but I would like this class x to be numbered at each replay, is there such a possibility?

Code sample:

<?php
                    foreach ($list_menu as $key => $menu):
                    ?>
                            <?php
                            if(count($menu->subMenu)):
                            ?>
                                <li class="dropdown">
                                    <a href="<?= base_url('categoria/' . $menu->txtUrlMenuSite); ?>"><?= strtolower($menu->txtMenu); ?></a>
                                    <div class="dropdown-content">
                                        <div class="col-xs-12">
                                            <ul class="sub-nav">
                                                <?php
                                                foreach ($menu->subMenu as $key => $sub):
                                                ?>
                                                    <li><a href="<?= base_url('categoria/' . $sub->txtUrlMenuSite); ?>"><?= $sub->txtMenu; ?></a></li>
                                                <?php
                                                endforeach;
                                                ?>
                                            </ul>
                                        </div>
                                    </div>
                                </li>
                            <?php
                            else:
                            ?>
                                <li><a href="<?= base_url('categoria/' . $menu->txtUrlMenuSite); ?>"><?= strtolower($menu->txtMenu); ?></a></li>
                            <?php
                            endif;
                            ?>
                        </li>
                    <?php
                    endforeach;
                    ?>
    
asked by anonymous 20.03.2018 / 17:59

1 answer

1

Gustavo,

you can do:

$a = "";
foreach ($arr as $key => $value)
   $a .= "<div><div class='x_".$key."'></div></div>";

 echo $a;

or put a counter:

$count = 0;
$a = "";
foreach ($arr as $value)
    $a .= "<div><div class='x_".$count++."'></div></div>";

echo $a;
    
20.03.2018 / 18:07