我需要在单行中获取用户的表阶段明智计数应该是动态的

 Actual i get

Array (
    [person1] => Array
        (
            [stage1] => 4
            [stage2] => 4
            [stage3] => 4
            [stage4] => 4
            [stage5] => 2
        )

    [person2] => Array
        (
            [stage3] => 1
        )

    [person3] => Array
        (
            [stage1] => 2
            [stage2] => 1
            [stage4] => 1
            [stage5] => 2
        )

)

1)这里有5个阶段stage1,stage2...stage5

2)他们是3个用户 person1 person2 person3

有些人没有阶段,请在该阶段保持为零或为空

将此数组隐藏到表中,如下图所示,请帮助我

https://i.stack.imgur.com/ccJLg.png


慕勒3428872
浏览 99回答 1
1回答

临摹微笑

首先,如果事先不知道并且可能是动态的,则需要找到所有阶段。如果有的话,您可以通过迭代数据来构建表。<?php$data = [&nbsp; 'person1' => [&nbsp; &nbsp; 'stage1' => 4,&nbsp; &nbsp; 'stage2' => 4,&nbsp; &nbsp; 'stage3' => 4,&nbsp; &nbsp; 'stage4' => 4,&nbsp; &nbsp; 'stage5' => 2,&nbsp; ],&nbsp; 'person2' => [&nbsp; &nbsp; 'stage3' => 1,&nbsp; ],&nbsp; 'person3' => [&nbsp; &nbsp; 'stage1' => 2,&nbsp; &nbsp; 'stage2' => 1,&nbsp; &nbsp; 'stage4' => 1,&nbsp; &nbsp; 'stage5' => 2,&nbsp; ],];$stages = [];foreach ($data as $person) {&nbsp; foreach ($person as $stage => $number) {&nbsp; &nbsp; if (!in_array($stage, $stages)) {&nbsp; &nbsp; &nbsp; $stages[] = $stage;&nbsp; &nbsp; }&nbsp; }}var_dump($stages);?><table>&nbsp; <th>person</th>&nbsp; <?php foreach ($stages as $stage): ?>&nbsp; &nbsp; <th><?php echo $stage; ?></th>&nbsp; <?php endforeach ?>&nbsp; <?php foreach ($data as $personName => $person): ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><?php echo $personName; ?></td>&nbsp; &nbsp; &nbsp; <?php foreach ($stages as $stage): ?>&nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $person[$stage] ?? 0; ?></td>&nbsp; &nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; </tr>&nbsp; <?php endforeach; ?></table>结果:array(5) { [0]=> string(6) "stage1" [1]=> string(6) "stage2" [2]=> string(6) "stage3" [3]=> string(6) "stage4" [4]=> string(6) "stage5" }person&nbsp; stage1&nbsp; stage2&nbsp; stage3&nbsp; stage4&nbsp; stage5person1&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;2person2&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0person3&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;2
打开App,查看更多内容
随时随地看视频慕课网APP