亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要在單行中獲取用戶的表階段明智計數應該是動態的

我需要在單行中獲取用戶的表階段明智計數應該是動態的

PHP
慕勒3428872 2023-10-22 21:45:42
 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...stage52)他們是3個用戶 person1 person2 person3有些人沒有階段,請在該階段保持為零或為空將此數組隱藏到表中,如下圖所示,請幫助我https://i.stack.imgur.com/ccJLg.png
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

首先,如果事先不知道并且可能是動態的,則需要找到所有階段。如果有的話,您可以通過迭代數據來構建表。


<?php


$data = [

  'person1' => [

    'stage1' => 4,

    'stage2' => 4,

    'stage3' => 4,

    'stage4' => 4,

    'stage5' => 2,

  ],

  'person2' => [

    'stage3' => 1,

  ],

  'person3' => [

    'stage1' => 2,

    'stage2' => 1,

    'stage4' => 1,

    'stage5' => 2,

  ],

];


$stages = [];

foreach ($data as $person) {

  foreach ($person as $stage => $number) {

    if (!in_array($stage, $stages)) {

      $stages[] = $stage;

    }

  }

}


var_dump($stages);


?>

<table>

  <th>person</th>

  <?php foreach ($stages as $stage): ?>

    <th><?php echo $stage; ?></th>

  <?php endforeach ?>

  <?php foreach ($data as $personName => $person): ?>

    <tr>

      <td><?php echo $personName; ?></td>

      <?php foreach ($stages as $stage): ?>

        <td><?php echo $person[$stage] ?? 0; ?></td>

      <?php endforeach; ?>

    </tr>

  <?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  stage1  stage2  stage3  stage4  stage5

person1      4       4       4       4       2

person2      0       0       1       0       0

person3      2       1       0       1       2


查看完整回答
反對 回復 2023-10-22
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號