如何使用模数格式化不同的数组以在 table 中列出?

How to format different arrays to list out in table using modulo?

这是我的数组

$headers = '
    '2020-02-10' => [
        'DateShort' => 'Mon 10/02/2020',
    ],
    '2020-02-11' => [
        'DateShort' => 'Tue 11/02/2020',
    ],
    '2020-02-12' => [
        'DateShort' => 'Wed 12/02/2020',
    ],
    '2020-02-13' => [
        'DateShort' => 'Thu 13/02/2020',
    ],
    '2020-02-14' => [
        'DateShort' => 'Fri 14/02/2020',
    ],
    '2020-02-15' => [
        'DateShort' => 'Sat 15/02/2020',
    ],
    '2020-02-16' => [
        'DateShort' => 'Sun 16/02/2020',
    ],
    '2020-02-17' => [
        'DateShort' => 'Mon 17/02/2020',
    ],
    '2020-02-18' => [
        'DateShort' => 'Tue 18/02/2020',
    ],
    '2020-02-19' => [
        'DateShort' => 'Wed 19/02/2020',
    ],
];

$data = [
    [
        'RoomName' => 'RoomName1'
        'Days' => [
            '2020-02-10' => [
                    'NumRooms' => 10,
                    'AverageRate' => 'INR10.00',
                    'AverageRateText' =>  @ INR10.00
                ],
            '2020-02-11' => [
                    'NumRooms' => 20,
                    'AverageRate' => 'INR20.00',
                    'AverageRateText' =>  @ INR20.00
                ],
            '2020-02-12' => [
                    'NumRooms' => 30,
                    'AverageRate' => 'INR30.00',
                    'AverageRateText' =>  @ INR30.00
                ],
            '2020-02-13' => [
                    'NumRooms' => 40,
                    'AverageRate' => 'INR40.00',
                    'AverageRateText' =>  @ INR40.00
                ],
            '2020-02-14' => [
                    'NumRooms' => 50
                    'AverageRate' => INR5.00
                    'AverageRateText' =>  @ INR50.00
                ],
            '2020-02-15' => [
                    'NumRooms' => 60
                    'AverageRate' => INR60.00
                    'AverageRateText' =>  @ INR60.00
                ],
            '2020-02-16' => [
                    'NumRooms' => 70
                    'AverageRate' => INR70.00
                    'AverageRateText' =>  @ INR70.00
                ],
            '2020-02-17' => [
                    'NumRooms' => 80
                    'AverageRate' => INR80.00
                    'AverageRateText' =>  @ INR80.00
                ],
            '2020-02-18' => [
                    'NumRooms' => 90
                    'AverageRate' => INR90.00
                    'AverageRateText' =>  @ INR90.00
                ],
            '2020-02-19' => [
                    'NumRooms' => 100
                    'AverageRate' => INR100.00
                    'AverageRateText' =>  @ INR100.00
                ],
        ],
    ],
    [
        'RoomName' => 'RoomName2'
        'Days' => [
            '2020-02-10' => [
                    'NumRooms' => 100,
                    'AverageRate' => 'INR100.00',
                    'AverageRateText' =>  @ INR100.00
                ],
            '2020-02-11' => [
                    'NumRooms' => 200,
                    'AverageRate' => 'INR200.00',
                    'AverageRateText' =>  @ INR200.00
                ],
            '2020-02-12' => [
                    'NumRooms' => 300,
                    'AverageRate' => 'INR300.00',
                    'AverageRateText' =>  @ INR300.00
                ],
            '2020-02-13' => [
                    'NumRooms' => 400,
                    'AverageRate' => 'INR400.00',
                    'AverageRateText' =>  @ INR400.00
                ],
            '2020-02-14' => [
                    'NumRooms' => 500
                    'AverageRate' => INR500.00
                    'AverageRateText' =>  @ INR500.00
                ],
            '2020-02-15' => [
                    'NumRooms' => 600
                    'AverageRate' => INR600.00
                    'AverageRateText' =>  @ INR600.00
                ],
            '2020-02-16' => [
                    'NumRooms' => 700
                    'AverageRate' => INR700.00
                    'AverageRateText' =>  @ INR700.00
                ],
            '2020-02-17' => [
                    'NumRooms' => 800
                    'AverageRate' => INR800.00
                    'AverageRateText' =>  @ INR800.00
                ],
            '2020-02-18' => [
                    'NumRooms' => 900
                    'AverageRate' => INR900.00
                    'AverageRateText' =>  @ INR900.00
                ],
            '2020-02-19' => [
                    'NumRooms' => 1000
                    'AverageRate' => INR1000.00
                    'AverageRateText' =>  @ INR1000.00
                ],
        ]
    ],
    [
        'RoomName' => 'RoomName3'
        'Days' => [
            '2020-02-10' => [
                    'NumRooms' => 100,
                    'AverageRate' => 'INR100.00',
                    'AverageRateText' =>  @ INR100.00
                ],
            '2020-02-11' => [
                    'NumRooms' => 200,
                    'AverageRate' => 'INR200.00',
                    'AverageRateText' =>  @ INR200.00
                ],
            '2020-02-12' => [
                    'NumRooms' => 300,
                    'AverageRate' => 'INR300.00',
                    'AverageRateText' =>  @ INR300.00
                ],
            '2020-02-13' => [
                    'NumRooms' => 400,
                    'AverageRate' => 'INR400.00',
                    'AverageRateText' =>  @ INR400.00
                ],
            '2020-02-14' => [
                    'NumRooms' => 500
                    'AverageRate' => INR500.00
                    'AverageRateText' =>  @ INR500.00
                ],
            '2020-02-15' => [
                    'NumRooms' => 600
                    'AverageRate' => INR600.00
                    'AverageRateText' =>  @ INR600.00
                ],
            '2020-02-16' => [
                    'NumRooms' => 700
                    'AverageRate' => INR700.00
                    'AverageRateText' =>  @ INR700.00
                ],
            '2020-02-17' => [
                    'NumRooms' => 800
                    'AverageRate' => INR800.00
                    'AverageRateText' =>  @ INR800.00
                ],
            '2020-02-18' => [
                    'NumRooms' => 900
                    'AverageRate' => INR900.00
                    'AverageRateText' =>  @ INR900.00
                ],
            '2020-02-19' => [
                    'NumRooms' => 1000
                    'AverageRate' => INR1000.00
                    'AverageRateText' =>  @ INR1000.00
                ],
        ]
    ]
];

我想把它格式化成这种形式

<table cellspacing="0" cellpadding="0" border="1">
   <tr>
      <th></th>
      <th>Mon 10/02/2020</th>
      <th>Tue 11/02/2020</th>
      <th>Wed 12/02/2020</th>
      <th>Thu 13/02/2020</th>
      <th>Fri 14/02/2020</th>
   </tr>
   <tr>
      <td>RoomName1</td>
      <td>10 @ INR10.00</td>
      <td>20 @ INR20.00</td>
      <td>30 @ INR30.00</td>
      <td>40 @ INR40.00</td>
      <td>50 @ INR50.00</td>
   </tr>
   <tr>
      <td>RoomName2</td>
      <td>100 @ INR100.00</td>
      <td>200 @ INR200.00</td>
      <td>300 @ INR300.00</td>
      <td>400 @ INR400.00</td>
      <td>500 @ INR500.00</td>
   </tr>
   <tr>
      <td>RoomName3</td>
      <td>100 @ INR100.00</td>
      <td>200 @ INR200.00</td>
      <td>300 @ INR300.00</td>
      <td>400 @ INR400.00</td>
      <td>500 @ INR500.00</td>
   </tr>
   <tr>
      <th></th>
      <th>Sat 15/02/2020</th>
      <th>Sun 16/02/2020</th>
      <th>Mon 17/02/2020</th>
      <th>Tue 18/02/2020</th>
      <th>Wed 19/02/2020</th>
  </tr>
  <tr>
        <td>RoomName1</td>
      <td>60 @ INR60.00</td>
      <td>70 @ INR7.00</td>
      <td>80 @ INR80.00</td>
      <td>90 @ INR90.00</td>
      <td>100 @ INR100.00</td>
  </tr>
  <tr>
    <td>RoomName2</td>
      <td>600 @ INR600.00</td>
      <td>700 @ INR700.00</td>
      <td>800 @ INR800.00</td>
      <td>900 @ INR900.00</td>
      <td>1000 @ INR1000.00</td>
  </tr>
  <tr>
    <td>RoomName3</td>
      <td>600 @ INR600.00</td>
      <td>700 @ INR700.00</td>
      <td>800 @ INR800.00</td>
      <td>900 @ INR900.00</td>
      <td>1000 @ INR1000.00</td>
  </tr>
</table>

我写的代码:

$html .= '<table cellspacing="0" cellpadding="0" border="1">';

$columnIterator = 1;
$headerData = [];
$headerCols = 0;
foreach ($headerdatas as $sqlDate => $data) {
    if ($columnIterator % 5 == 1) {
        $headerData[$headerCols] = '<tr><th></th>';
    }
    if (!is_array($data)) {
        continue;
    }
    $headerData[$headerCols] .= '<th>' . $data['DateShort'] . '</th>';
    $dates[] = $sqlDate;
    if ($columnIterator % 5 == 0) {
        $headerData[$headerCols] .= '</tr>';
        $headerCols++;
    }
    $columnIterator++;
}

$tableRows = 0;
foreach ($tablerows as $roomData) {
    $columnIterator = 1;
    $html .= $headerData[$tableRows];
    foreach ($dates as $sqlDate) {
        if ($columnIterator % 5 == 1) {
            $html .=  '<tr><td>' . $roomData['RoomName'] . '</td>';
        }
        $html .= '<td>';
        if (array_key_exists($sqlDate, $roomData['Days'])) {
            $html .= $roomData['Days'][$sqlDate]['NumRooms']
                . $roomData['Days'][$sqlDate]['AverageRateText'];
        }
        else {
            $html .= '0';
        }
        $html .= '</td>';
        if ($columnIterator % 5 == 0) {
            $html .= '</tr>';
        }
        $columnIterator++;
    }
    $tableRows++;
    $html .= '</tr>';
}
$html .= '</table>';

这就是我认为你想要的,我必须为你修复很多错误,因为你的 roomDataDates 不是一个有效的数组,而且你发布的 PHP 中的一些逻辑是不对,就像在 $tableRows 中循环,而您已经为其分配了 0:

的值
<?php 
$headers = [
    '2020-02-10' => [
        'DateShort' => 'Mon 10/02/2020',
    ],
    '2020-02-11' => [
        'DateShort' => 'Tue 11/02/2020',
    ],
    '2020-02-12' => [
        'DateShort' => 'Wed 12/02/2020',
    ],
    '2020-02-13' => [
        'DateShort' => 'Thu 13/02/2020',
    ],
    '2020-02-14' => [
        'DateShort' => 'Fri 14/02/2020',
    ],
    '2020-02-15' => [
        'DateShort' => 'Sat 15/02/2020',
    ],
    '2020-02-16' => [
        'DateShort' => 'Sun 16/02/2020',
    ],
    '2020-02-17' => [
        'DateShort' => 'Mon 17/02/2020',
    ],
    '2020-02-18' => [
        'DateShort' => 'Tue 18/02/2020',
    ],
    '2020-02-19' => [
        'DateShort' => 'Wed 19/02/2020',
    ],
];

$roomsDataDates = [
    [
        'RoomName' => 'RoomName1',
        'Days' => [
            '2020-02-10' => [
                    'NumRooms' => 10,
                    'AverageRate' => 'INR10.00',
                    'AverageRateText' =>  '@ INR10.00'
                ],
            '2020-02-11' => [
                    'NumRooms' => 20,
                    'AverageRate' => 'INR20.00',
                    'AverageRateText' =>  '@ INR20.00'
                ],
            '2020-02-12' => [
                    'NumRooms' => 30,
                    'AverageRate' => 'INR30.00',
                    'AverageRateText' =>  '@ INR30.00'
                ],
            '2020-02-13' => [
                    'NumRooms' => 40,
                    'AverageRate' => 'INR40.00',
                    'AverageRateText' =>  '@ INR40.00'
                ],
            '2020-02-14' => [
                    'NumRooms' => 50,
                    'AverageRate' => 'INR5.00',
                    'AverageRateText' =>  '@ INR50.00'
                ],
            '2020-02-15' => [
                    'NumRooms' => 60,
                    'AverageRate' => 'INR60.00',
                    'AverageRateText' =>  '@ INR60.00'
                ],
            '2020-02-16' => [
                    'NumRooms' => 70,
                    'AverageRate' => 'INR70.00',
                    'AverageRateText' =>  '@ INR70.00'
                ],
            '2020-02-17' => [
                    'NumRooms' => 80,
                    'AverageRate' => 'INR80.00',
                    'AverageRateText' =>  '@ INR80.00'
                ],
            '2020-02-18' => [
                    'NumRooms' => 90,
                    'AverageRate' => 'INR90.00',
                    'AverageRateText' =>  '@ INR90.00'
                ],
            '2020-02-19' => [
                    'NumRooms' => 100,
                    'AverageRate' => 'INR100.00',
                    'AverageRateText' =>  '@ INR100.00'
                ],
        ],
    ],
    [
        'RoomName' => 'RoomName2',
        'Days' => [
            '2020-02-10' => [
                    'NumRooms' => 100,
                    'AverageRate' => 'INR100.00',
                    'AverageRateText' =>  '@ INR100.00'
                ],
            '2020-02-11' => [
                    'NumRooms' => 200,
                    'AverageRate' => 'INR200.00',
                    'AverageRateText' =>  '@ INR200.00'
                ],
            '2020-02-12' => [
                    'NumRooms' => 300,
                    'AverageRate' => 'INR300.00',
                    'AverageRateText' =>  '@ INR300.00'
                ],
            '2020-02-13' => [
                    'NumRooms' => 400,
                    'AverageRate' => 'INR400.00',
                    'AverageRateText' =>  '@ INR400.00'
                ],
            '2020-02-14' => [
                    'NumRooms' => 500,
                    'AverageRate' => 'INR500.00',
                    'AverageRateText' =>  '@ INR500.00'
                ],
            '2020-02-15' => [
                    'NumRooms' => 600,
                    'AverageRate' => 'INR600.00',
                    'AverageRateText' =>  '@ INR600.00'
                ],
            '2020-02-16' => [
                    'NumRooms' => 700,
                    'AverageRate' => 'INR700.00',
                    'AverageRateText' =>  '@ INR700.00'
                ],
            '2020-02-17' => [
                    'NumRooms' => 800,
                    'AverageRate' => 'INR800.00',
                    'AverageRateText' =>  '@ INR800.00'
                ],
            '2020-02-18' => [
                    'NumRooms' => 900,
                    'AverageRate' => 'INR900.00',
                    'AverageRateText' =>  '@ INR900.00'
                ],
            '2020-02-19' => [
                    'NumRooms' => 1000,
                    'AverageRate' => 'INR1000.00',
                    'AverageRateText' =>  '@ INR1000.00'
                ],
        ]
    ]
];

$html = '<table cellspacing="0" cellpadding="0" border="1">';

$columnIterator = 1;
$headerData = [];
$headerCols = 0;

function createRow($data, $dates, $start, $length, $roomData)
{
    $html = "<tr><td>$roomData</td>";
    // To display data under the next row of headings.
    for ($i = $start; $i < $start + $length; $i++) {
        $sqlDate = $dates[$i];
        $html .= '<td>';
        if (array_key_exists($sqlDate, $data['Days'])) {
            $html .= $data['Days'][$sqlDate]['NumRooms'] . ' '
                . $data['Days'][$sqlDate]['AverageRateText'];
        }
        else {
            $html .= '0';
        }
        $html .= '</td>';
    }
    $html .= '</tr>';
    return $html;
}

foreach ($headers as $sqlDate => $data) {
    if ($columnIterator % 5 == 1) {
        $headerData[$headerCols] = '<tr><th></th>';
    }
    if (!is_array($data)) {
        continue;
    }
    $headerData[$headerCols] .= '<th>' . $data['DateShort'] . '</th>';
    $dates[] = $sqlDate;
    if ($columnIterator % 5 == 0) {
        $headerData[$headerCols] .= '</tr>';
        $headerCols++;
    }
    $columnIterator++;
}
$rooms = array_values_deep($venueRooms[$roomVenueId], 'RoomName');
$tableRows = 0;
foreach ($headerData as $header) {
   $html .= $header;
   $columnIterator = 1;
   $k = 0;
   foreach ($rooms as $roomData) {
      $html .= createRow($roomsDataDates[$k], $dates, $tableRows * 5, 5, $roomData);
      $columnIterator++;
      $k++;
    }
    $tableRows++;
}
$html .= '</table>';

echo $html;