我有一个PHP shell_exec命令,为每个结果输出5行数据,如果shell_exec有1个结果,它将输出5行,如下所示:
约翰里士满27伦敦小狗该命令可能有更多……
运用 array_chunk ,你确实拥有你需要的大部分内容,你只需要预知你生成的数组,并使用一点点 list 魔术,分配你的变量。
array_chunk
list
简而言之,什么 list 正在做,正在逐个获取您的数组元素并将它们分配给您将在您的数组中定义的变量 list 声明。
<div class="table-responsive"> <?php $array = array_chunk( explode( PHP_EOL, shell_exec( "shell command" ) ), 5 ); ?> <table> <thead> <tr> <th>Name</th> <th>Surname</th> <th>Age</th> <th>City</th> <th>Pet</th> </tr> </thead> <tbody> <?php foreach($array as $element) { ?> <?php list($name, $surname, $age, $city, $pet) = $element; ?> <tr class="1"> <td class="1"> <i class="cc"><?php echo $name ?></i> </td> <td class="1"> <i class="cc"><?php echo $surname ?></i> </td> <td class="1"> <i class="cc"><?php echo $age ?></i> </td> <td class="1"> <i class="cc"><?php echo $city ?></i> </td> <td class="1"> <i class="cc"><?php echo $pet ?></i> </td> </tr> <?php } ?> </tbody> </table> </div>