-3

大批

$array['1']                         = null;

$array['2']['21']                   = null;

$array['3']['31']                   = null;
$array['3']['32']                   = null;

$array['4']['41']                   = null;
$array['4']['42']['421']            = null;
$array['4']['42']['422']            = null;

$array['5']['51']                   = null;
$array['5']['52']                   = null;
$array['5']['53']['531']            = null;
$array['5']['53']['532']            = null;
$array['5']['53']['533']            = null;

$array['6']['61']['611']            = null;
$array['6']['62']                   = null;
$array['6']['63']['631']            = null;
$array['6']['63']['632']            = null;
$array['6']['63']['633']            = null;
$array['6']['63']['634']            = null;

$array['7']['71']['711']            = null;

$array['8']['81']['811']            = null;
$array['8']['81']['812']            = null;

$array['9']['91']['911']            = null;
$array['9']['91']['912']            = null;
$array['9']['92']['921']            = null;
$array['9']['92']['922']            = null;

我正在尝试将上面的数组转换为 HTML 表格。我需要添加rowspan以获得如下图所示的输出,但果汁用完了。

PHP

//If condition is satistied
if (isset($array) && count($array) > 0)
{
    //echo '<pre>'; print_r($array); exit;

    //Initiate variables
    $row = null;
    $level_one_array = $array;

    //Iterate through menu level one
    foreach ($level_one_array as $level_one_name => $level_two_array)
    {
        //If level two is an array
        if (is_array($level_two_array))
        {
            //Iterate through menu level two
            foreach ($level_two_array as $level_two_name => $level_three_array)
            {
                //If level two is an array
                if (is_array($level_three_array))
                {
                    //Iterate through menu level three
                    foreach ($level_three_array as $level_three_name => $null)
                    {
                        $row .= '<tr>';
                        $row .= '<td class="td_data">' . $level_one_name . '</td>';
                        $row .= '<td class="td_data">' . $level_two_name . '</td>';
                        $row .= '<td class="td_data">' . $level_three_name . '</td>';
                        $row .= '</tr>';
                    }
                }
                //If level two is not an array
                else
                {
                    $row .= '<tr>';
                    $row .= '<td class="td_data">' . $level_one_name . '</td>';
                    $row .= '<td class="td_data">' . $level_two_name . '</td>';
                    $row .= '<td class="td_data">&nbsp;</td>';
                    $row .= '</tr>';
                }
            }
        }
        //If level two is not an array
        else
        {
            $row .= '<tr>';
            $row .= '<td class="td_data">' . $level_one_name . '</td>';
            $row .= '<td class="td_data">&nbsp;</td>';
            $row .= '<td class="td_data">&nbsp;</td>';
            $row .= '</tr>';
        }
    }

    $table  = '<table>';
    $table .= '<tr><td class="td_title">Menus</td><td class="td_title">Submenus/Items</td><td class="td_title">Items</td></tr>';
    $table .= $row;
    $table .= '</table>';

    echo $table;
    }
else
{
    echo '<center>No record found</center>';
}

我需要的是: 在此处输入图像描述

4

1 回答 1

1

我写了新的代码来解决这个问题。工作rowspan非常棘手。关键元素是calculate_rowspan()和标志$on_level_X_row来确定是否需要添加<tr>

有必要从最内部的 foreach 中分离输出,因为您必须使用它处理多行。"\n"我还在每个之后添加,</tr>以使输出更具可读性。

function calculate_rowspan($array)
{
    if (!is_array($array) || count($array) == 0)
        return 1;

    $rowspan = 0;
    foreach ($array as $key=>$value)
    {
        $rowspan++;
        if (is_array($value) && count($value) > 0)
        {
            $rowspan += count($value);
            // -1 because first element is one same row
            $rowspan--;
        }
    }
    return $rowspan;
}

if (is_array($array) && count($array) > 0)
{
    $output = '';
    foreach ($array as $level_one_name => $level_two_array)
    {
        if (!is_array($level_two_array) || count($level_two_array) == 0)
        {
            $output .= '<tr><td>';
            $output .= $level_one_name;
            $output .= '</td><td></td><td></td></tr>'."\n";
        }
        else
        {
            $output .= '<tr>';
            $output .= '<td rowspan="'.calculate_rowspan($level_two_array).'">';
            $output .= $level_one_name;
            $output .= '</td>';

            $on_level_one_row = TRUE;
            foreach ($level_two_array as $level_two_name => $level_three_array)
            {
                if ( ! $on_level_one_row)
                    $output .= '<tr>';
                else
                    $on_level_one_row = FALSE;

                if (!is_array($level_three_array) || count($level_three_array) == 0)
                {
                    $output .= '<td>';
                    $output .= $level_two_name;
                    $output .= '</td><td></td></tr>'."\n";
                }
                else
                {
                    $output .= '<td rowspan="'.calculate_rowspan($level_three_array).'">';
                    $output .= $level_two_name;
                    $output .= '</td>';

                    $on_level_two_row = TRUE;
                    foreach ($level_three_array as $level_three_name => $null)
                    {
                        if ( ! $on_level_two_row)
                            $output .= '<tr>';
                        else
                            $on_level_two_row = FALSE;

                        $output .= '<td>';
                        $output .= $level_three_name;
                        $output .= '</td></tr>'."\n";
                    }
                }
            }
        }
    }

    print '<table>'."\n";
    print $output;
    print '</table>';
}
于 2013-03-28T15:41:04.907 回答