将3D数组转换为HTML表

时间:2013-03-28 14:43:52

标签: php html

ARRAY

$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>';
}

我需要的是: enter image description here

1 个答案:

答案 0 :(得分: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>';
}