从数组重新创建html页面(2.0)

时间:2016-06-16 10:20:10

标签: php html arrays foreach tags

大家早上好, 昨天我问了一个问题来解决这个问题,但阵列存在缺陷。 现在数组似乎已修复,但我在创建页面时遇到了麻烦。 所以,这是固定数组

'tag' => array(
    'tagName' => 'section',
    'tagAttributes' => array(
        'class' => 'main full',
    ),
    'tagContents' => array(
        'tag' => array(
            'tagName' => 'img',
            'tagAttributes' => array(
                'class' => 'logo',
                'src' => 'assets/images/logo-480x176.png'
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'cover sfondo'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'img',
                    'tagAttributes' => array(
                        'class' => 'cover-image',
                        'src' => 'assets/images/cover-img-header.png'
                    )
                )
            )
        )
    ),
    'tag_2' => array(
        'tagName' => 'div',
        'tagAttributes' => array(
            'class' => 'cover-mob sfondo-mob'
        ),
        'tagContents' => array(
            'tag' => array(
                'tagName' => 'img',
                'tagAttributes' => array(
                    'class' => 'cover-image-mob',
                    'src' => 'assets/images/cover-img-mobile.jpg'
                )
            )
        )
    ),
    'tag_3' => array(
        'tagName' => 'a',
        'tagAttributes' => array(
            'class' => 'button order light'
        ),
        'tagContents' => array(
            'tag' => array(
                'tagName' => 'div',
                'tagAttributes' => array(
                    'class' => 'data-min',
                ),
                'tagContents' => array(
                    'tag' => array(
                        'tagName' => 'div',
                        'tagAttributes' => array(
                            'class' => 'label-holder',
                        ),
                        'tagContents' => array(
                            'tag' => array(
                                'tagName' => 'img',
                                'tagAttrbiutes' => array(
                                    'src' => 'assets/images/sfondo-cta.jpg'
                                )
                            ),
                            'tag_1' => array(
                                'tagName' => 'p',
                                'tagAttributes' => array(
                                    'class' => 'label',
                                ),
                                'tagContents' => array(
                                    'strong' => 'text'
                                )
                            )
                        )
                    ),
                    'tag_1' => array(
                        'tagName' => 'div',
                        'tagAttributes' => array(
                            'class' => 'price-holder'
                        )
                    )
                )
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'cta'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'p',
                    'tagAttributes' => array(
                        'class' => 'main-title'
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'a',
                            'tagAttributes' => array(
                                'strong' => 'text'
                            )
                        )
                    )
                )
            )
        ),
        'tag_2' => array(
            'tagName' => 'img',
            'tagAttributes' => array(
                'class' => 'main-title',
                'src' => 'assets/images/extra_1.png'
            )
        )
    )
),
'tag_1' => array(
    'tagName' => 'section',
    'tagAttributes' => array(
        'class' => 'gallery',
        'id' => 'video'
    ),
    'tagContents' => array(
        'tag' => array(
            'tagName' => 'p',
            'tagAttributes' => array(
                'class' => 'title video-title',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'span',
                    'tagAttributes' => array(
                        'strong' => 'text'
                    )
                )
            )
        ),
        'tag_1' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'container',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'div',
                    'tagAttributes' => array(
                        'class' => 'arrow-container prev',
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'img',
                            'tagAttributes' => array(
                                'class' => 'arrow',
                                'src' => 'assets/images/freccia-sx_1.png'
                            )
                        )
                    )
                )
            )
        ),
        'tag_2' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'arrow-container next',
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'img',
                    'tagAttributes' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-dx_1.png'
                    )
                )
            )
        ),
        'tag_3' => array(
            'tagName' => 'div',
            'tagAttributes' => array(
                'class' => 'gallery-holder'
            ),
            'tagContents' => array(
                'tag' => array(
                    'tagName' => 'div',
                    'tagAttributes' => array(
                        'class' => 'internal single'
                    ),
                    'tagContents' => array(
                        'tag' => array(
                            'tagName' => 'div',
                            'tagAttributes' => array(
                                'class' => 'gallery-item video',
                            ),
                            'tagContents' => array(
                                'tag' => array(
                                    'tagName' => 'div',
                                    'tagAttributes' => array(
                                        'class' => 'wrapper video-wrapper'
                                    ),
                                    'tagContents' => array(
                                        'tag' => array(
                                            'tagName' => 'img',
                                            'tagAttributes' => array(
                                                'class' => 'play-vid pointer thumb',
                                                'src' => 'assets/images/img-spot-tv.png'
                                            )
                                        ),
                                        'tag_1' => array(
                                            'tagName' => 'img',
                                            'tagAttributes' => array(
                                                'class' => 'play-icon hover',
                                                'src' => 'assets/images/play.png'
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )
),

这就是我尝试创建html页面的方式

foreach ($MiniSiteStructureArray as $section => $sectionStructure) {

    $html = '<' . $sectionStructure['tagName'] . '';
    foreach ($sectionStructure['tagAttributes'] as $name => $value) {
        $html .= " $name='" . $value . "'>";
    }
    foreach ($sectionStructure['tagContents'] as $contents) {
        foreach ($contents['tagAttributes'] as $name => $value) {
            $html .= '<' . $contents['tagName'] . ' ' . $name . '="' . $value . '">';
        }
        if (isset($contents['tagContents'])) {
            $html .= findAllContents($contents['tagContents'], $html);
        }
    }
}

function findAllContents($tagContents, $tmpHtml) {
    foreach ($tagContents as $tag => $tagValue) {
        if (isset($tagValue['tagAttributes'])) {
            foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
                $tmpHtml = '<' . $tagValue['tagName'] . ' ' . $tagKey . '="' . $value . '"></' . $tagValue['tagName'] . '>';
            }
        }
    }
    return $tmpHtml;
}

echo $html;

但是有一些问题。

Cycling&#39; tagAttributes&#39;例如。

1)代码找到&#39; class&#39; =&GT; &#39;标志&#39;但后来它发现了'src&#39; =&GT; &#39;资产/...&# 39;并覆盖以前的值。

2)同样的事情与$ html

Edit_1:

   foreach ($MiniSiteStructureArray as $section => $sectionStructure) {
    $html = '<' . $sectionStructure['tagName'] . '';
    foreach ($sectionStructure['tagAttributes'] as $name => $value) {
        $html .= " $name='" . $value . "'>";
    }
    foreach ($sectionStructure['tagContents'] as $contents) {
        $html .= '<' . $contents['tagName'];
        foreach ($contents['tagAttributes'] as $name => $value) {
            $html.=' '. $name . '="' . $value . '">';
        }
        if (isset($contents['tagContents'])) {
            $html .= findAllContents($contents['tagContents'], $html);
        }
    }
    $html .= '</'.$contents['tagName'].'>';   
}

function findAllContents($tagContents, $tmpHtml) {
    foreach ($tagContents as $tag => $tagValue) {
        $tmpHtml = '<' . $tagValue['tagName'];

        if (isset($tagValue['tagAttributes'])) {
            foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
                $tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
            }
        }

        $tmpHtml .= '></' . $tagValue['tagName'] . '>';
    }
    return $tmpHtml;
}

更好,谢谢。

现在仍然是$ html问题。 我一直在考虑在代码的开头打开一个像这个$html = <html><head></head><body>的html标签,最后关闭html标签和body标签,但我尝试过并且不起作用。

Edit_2:

好的,我认为已经完成了。我只在开头添加了这个$html = ''

1 个答案:

答案 0 :(得分:1)

您必须在tagAttributes foreach循环之前打开您的标记,然后关闭它。如果不将tagAttributes键添加到数组中也会出现问题,因此标记本身将被省略。

修改findAllContents()的foreach循环的示例:

foreach ($tagContents as $tag => $tagValue) {
    $tmpHtml .= '<' . $tagValue['tagName']

    if (isset($tagValue['tagAttributes'])) {
        foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
            $tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
        }
    }

    $tmpHtml .= '></' . $tagValue['tagName'] . '>';
}