将嵌套的关联数组键打印为行号

Print nested associative array keys as row numbers

我有一个嵌套的关联数组,可以在 table 中打印出用户数据。这是代码:

<table class="table table-striped">
    <thead>
        <tr>
            <th>#</th>
            <th>Prenume</th>
            <th>Nume de familie</th>
            <th>Email</th>
            <th>Telefon</th>
            <th>Oras</th>
            <th>Adresa</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($user_data as $arr){ ?>
                <tr>
                    <td>
                        row number nedded here
                    </td>                           
                    <?php foreach ($arr as $key => $value){ ?>
                    <td><?php echo $value; ?></td>
                    <?php } ?>
            </tr>
        <?php }?>
    </tbody>
</table>

我需要在最左边显示行号,而不是"row number nedded here"

您可以只添加一个变量作为计数器并显示:

<tbody>
    <?php $counter=0; foreach ($user_data as $arr){ ?>
        <tr>
            <td>
                <?php echo ++$counter; ?>
            </td>                           
            <?php foreach ($arr as $key => $value){ ?>
                <td><?php echo $value; ?></td>
            <?php } ?>
            </tr>
    <?php }?>
</tbody>

你可以这样做,如果你没有任何键,你的索引号从 0 开始。

 <tbody>
        <?php foreach ($user_data as $key=>$arr){ ?>
            <tr>
                <td>
                    <?php echo $key+1 ;?>
                </td>
                <td>
                    <?php echo $arr["prenume"];?>
                </td>
                <td>
                    <?php echo $arr["nume"];?>
                </td>
                <td>
                    <?php echo $arr["email"];?>
                </td>
                ...............

        </tr>
    <?php }?>
   </tbody>