从数组重新创建html页面的结构

时间:2016-06-15 08:48:40

标签: php html arrays foreach tags

大家早上好, 我正在尝试重新创建一个从数组中提取数据的html页面。 这是数组

    $MiniSiteStructureArray = array(
    'section_1' => array(
        'class' => 'main full',
        'contents' => array(
            'img' => array(
                'class' => 'logo',
                'src' => 'assets/images/logo-480x176.png'
            ),
            'div' => array(
                'class' => 'cover sfondo',
                'img' => array(
                    'class' => 'cover-image',
                    'src' => 'assets/images/cover-img-header.png'
                )
            ),
            'div_1' => array(
                'class' => 'cover-mob sfondo-mob',
                'img' => array(
                    'class' => 'cover-image-mob',
                    'src' => 'assets/images/cover-img-mobile.jpg'
                )
            ),
            'a' => array(
                'class' => 'button order light',
                'div' => array(
                    'class' => 'data-min',
                    'div' => array(
                        'class' => 'label-holder',
                        'img' => array(
                            'src' => 'assets/images/sfondo-cta.jpg'
                        ),
                        'p' => array(
                            'class' => 'label',
                            'strong' => 'text'
                        )
                    ),
                    'div_1' => array(
                        'class' => 'price-holder'
                    )
                )
            ),
            'div_2' => array(
                'class' => 'cta',
                'p' => array(
                    'class' => 'main-title',
                    'a' => array(
                        'strong' => 'text'
                    )
                )
            ),
            'img_1' => array(
                'class' => 'main-title',
                'src' => 'assets/images/extra_1.png'
            )
        )
    ),
    'section_2' => array(
        'class' => 'gallery',
        'id' => 'video',
        'contents' => array(
            'p' => array(
                'class' => 'title video-title',
                'span' => array(
                    'strong' => 'text'
                )
            ),
            'div' => array(
                'class' => 'container',
                'div' => array(
                    'class' => 'arrow-container prev',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-sx_1.png'
                    )
                ),
                'div_1' => array(
                    'class' => 'arrow-container next',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-dx_1.png'
                    )
                ),
                'div_2' => array(
                    'class' => 'gallery-holder',
                    'div' => array(
                        'class' => 'internal single',
                        'div' => array(
                            'class' => 'gallery-item video',
                            'div' => array(
                                'class' => 'wrapper video-wrapper',
                                'img' => array(
                                    'class' => 'play-vid pointer thumb',
                                    'src' => 'assets/images/img-spot-tv.png'
                                ),
                                'img_1' => array(
                                    'class' => 'play-icon hover',
                                    'src' => 'assets/images/play.png'
                                )
                            )
                        )
                    )
                )
            )
        )
    ),
    'section_3' => array(
        'class' => 'gallery',
        'id' => 'image',
        'contents' => array(
            'p' => array(
                'class' => 'title image-title',
                'span' => array(
                    'strong' => array(
                        'strong' => 'text'
                    )
                )
            ),
            'div' => array(
                'class' => 'container',
                'div' => array(
                    'class' => 'arrow-container prev',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-sx_1.png'
                    )
                ),
                'div_1' => array(
                    'class' => 'arrow-container next',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-dx_1.png'
                    )
                ),
                'div_2' => array(
                    'class' => 'gallery-holder',
                    'div' => array(
                        'class' => 'internal single',
                        'div' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper image-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/img-1.png'
                                ),
                                'p' => array(
                                    'class' => 'caption image-caption',
                                    'span' => 'text'
                                )
                            )
                        )
                    ),
                    'div_1' => array(
                        'class' => 'internal single',
                        'div' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper image-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/img-2.png'
                                ),
                                'p' => array(
                                    'class' => 'caption image-caption',
                                    'span' => 'text'
                                )
                            )
                        )
                    ),
                    'div_2' => array(
                        'class' => 'internal single',
                        'div' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper image-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/img-3.png'
                                ),
                                'p' => array(
                                    'class' => 'caption image-caption',
                                    'span' => 'text'
                                )
                            )
                        )
                    ),
                    'div_i' => array(
                        'class' => 'internal single',
                        'div' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper image-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/img-i.png'
                                ),
                                'p' => array(
                                    'class' => 'caption image-caption',
                                    'span' => 'text'
                                )
                            )
                        )
                    ),
                )
            ),
            'div_1' => array(
                'class' => 'counter'
            )
        )
    ),
    'section_4' => array(
        'class' => 'main',
        'id' => 'info',
        'contents' => array(
            'div' => array(
                'class' => 'cover sfondo',
                'img' => array(
                    'class' => 'cover-image',
                    'src' => 'assets/images/collection-img.jpg'
                )
            ),
            'div' => array(
                'class' => 'cover-mob sfondo-mob',
                'img' => array(
                    'class' => 'cover-image-mob',
                    'src' => 'assets/images/collection-img-mobile.jpg'
                )
            ),
            'a' => array(
                'class' => 'button order light',
                'div' => array(
                    'class' => 'data-min',
                    'div' => array(
                        'class' => 'label-holder',
                        'img' => array(
                            'src' => 'assets/images/sfondo-cta.jpg'
                        ),
                        'p' => array(
                            'class' => 'label',
                            'strong' => 'text'
                        )
                    ),
                    'div_1' => array(
                        'class' => 'price-holder',
                    )
                )
            ),
            'p' => array(
                'class' => 'text-numero-1',
                'span' => array(
                    'strong' => '',
                    'strong_1' => 'text',
                    'strong_2' => 'text',
                    'strong_3' => array(
                        'class' => 'text-anziche',
                        'strike' => 'text'
                    )
                )
            )
        )
    ),
    'section_5' => array(
        'class' => 'banner resize',
        'id' => 'num1',
        'contents' => array(
            'div' => array(
                'class' => 'cover'
            ),
            'div_1' => array(
                'class' => 'container',
                'img' => array(
                    'class' => 'banner-image',
                    'src' => 'assets/images/number-1.png'
                ),
                'p' => array(
                    'class' => 'cta num1-cta',
                    'span' => array(
                        'a' => 'text'
                    )
                )
            )
        )
    ),
    'section_6' => array(
        'class' => 'gallery',
        'id' => 'gifts',
        'contents' => array(
            'p' => array(
                'class' => 'title gifts-title',
                'span' => array(
                    'strong' => array(
                        'strong' => 'text'
                    )
                )
            ),
            'div' => array(
                'class' => 'container',
                'div' => array(
                    'class' => 'arrow-container prev',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-sx_1.png'
                    )
                ),
                'div_1' => array(
                    'class' => 'arrow-container next',
                    'img' => array(
                        'class' => 'arrow',
                        'src' => 'assets/images/freccia-dx_1.png'
                    )
                ),
                'div_2' => array(
                    'class' => 'gallery-holder',
                    'div' => array(
                        'class' => 'internal double',
                        'div' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper gifts-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/gift-1.jpg'
                                ),
                                'p' => array(
                                    'class' => 'caption gifts-caption',
                                    'span' => 'text'
                                )
                            )
                        ),
                        'div_1' => array(
                            'class' => 'gallery-item',
                            'div' => array(
                                'class' => 'wrapper gifts-wrapper',
                                'img' => array(
                                    'class' => 'thumb',
                                    'src' => 'assets/images/gift-2.jpg'
                                ),
                                'p' => array(
                                    'class' => 'caption gifts-caption',
                                    'span' => 'text'
                                )
                            )
                        )
                    )
                )
            ),
            'div_1' => array(
                'class' => 'counter'
            ),
            'p' => array(
                'class' => 'disclaimer gifts-disclaimer',
                'span' => array(
                    'span' => 'text'
                )
            )
        )
    ),
    'section_7' => array(
        'class' => 'footer',
        'contents' => array(
            'div' => array(
                'class' => 'container',
                'div' => array(
                    'class' => 'box',
                    'div' => array(
                        'class' => 'wrapper',
                        'p' => array(
                            'class' => 'icon pointer',
                            'img' => array(
                                'src' => 'assets/images/icn-piano-opera.png'
                            )
                        ),
                        'a' => array(
                            'class' => 'link',
                            'p' => array(
                                'class' => 'title',
                                'span' => 'text'
                            )
                        )
                    )
                ),
                'div_1' => array(
                    'class' => 'box',
                    'div' => array(
                        'class' => 'wrapper',
                        'p' => array(
                            'class' => 'icon pointer',
                            'img' => array(
                                'src' => 'assets/images/icn-piano-opera.png'
                            )
                        ),
                        'a' => array(
                            'class' => 'link gtm-externallinks',
                            'p' => array(
                                'class' => 'title',
                                'span' => 'text'
                            )
                        )
                    )
                )
            )
        )
    ),
    'section_8' => array(
        'class' => 'partners resize',
        'contents' => array(
            'div' => array(
                'class' => 'container',
                'img' => array(
                    'class' => 'partner',
                    'src' => 'assets/images/logo-dinsey-lucasfilm_2_1.png'
                ),
                'img_1' => array(
                    'class' => 'partner',
                    'src' => 'assets/images/logo-mondadori_1_1.png'
                )
            )
        )
    ),
    'section9' => array(
        'class' => 'partners-light',
        'contents' => array(
            'div' => array(
                'class' => 'container',
                'img' => array(
                    'class' => 'partner',
                    'src' => 'assets/images/copyrightLucasLight.png'
                )
            )
        )
    ));
 foreach ($MiniSiteStructureArray as $section => $sectionStructure) {
    foreach ($sectionStructure['contents'] as $tag => $contents) {
        $tags[] = $tag;
    }
    $a = formatTag($tags);
    $sections[] = $section;
}
$b = formatTag($sections);

你可以看到html标签格式不正确(例如div和div_1),所以我创建了这个函数

function formatTag($tags) {
$correctTags = array();
foreach ($tags as $tag) {
    switch ($tag) {
        case strpos($tag, "img"):
            $tag = "img";
            break;
        case strpos($tag, "div"):
            $tag = "div";
            break;
        case strpos($tag, "a"):
            $tag = "a";
            break;
        case strpos($tag, "p"):
            $tag = "p";
            break;
        case strpos($tag, "section"):
            $tag = "section";
            break;
    }
    array_push($correctTags, $tag);
}
return $correctTags;
}

所以我的想法是用新键(格式良好的标签)重新创建数组。 但现在我被卡住了。 假设已完成newKeysArray并假设创建section_1。 我能怎么做?

由于

编辑:新数组

    $structureArray = array(
    'tag' => 'section_8',
    'class' => 'partners resize',
    'contents' => array(
        'tag' => 'div',
        'class' => 'container',
        'contents' => array(
            'tag' => 'img',
            'class' => 'partner',
            'src' => 'assets/images/logo-dinsey-lucasfilm_2_1.png'
        )
    )
);

更好?

Edit_2:NewNewArray

  $structureArray = array(
    'tag' => array(
        'tagName' => 'section_1',
        'class' => 'main full',
        'contents' => array(
            'tag' => array(
                'tagName' => 'img',
                'class' => 'logo',
                'src' => 'assets/images/logo-480x176.png'
            ),
        ),
    ),
    'tag_1' => array(
        'tagName' => 'div',
        'class' => 'cover sfondo',
        'contents' => array(
            'tag' => array(
                'tagName' => 'img',
                'class' => 'cover-image',
                'src' => 'assets/images/cover-img-header.png'
            )
        ),
    ),
);

这应该更好,你不觉得吗? 在这种情况下,我手动增加了键标签,因为在html页面中它没有出现

Edit_3_I_hope:NewNewNewArray

 $structureArray = array(
    'tag' => array(
        'tagName' => 'section_1',
        'tagAttributes' => array(
            'class' => 'main full',
        ),
        'contents' => 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',
        ),
        'contents' => array(
            'tag' => array(
                'tagName' => 'img',
                'tagAttributes' => array(
                    'class' => 'cover-image',
                    'src' => 'assets/images/cover-img-header.png'
                )
            )
        ),
    ),
);

现在,它看起来非常好

1 个答案:

答案 0 :(得分:0)

我对阵列结构的建议:

$htmlArray = ['tag'   => ['name' => 'ul'],
              'attrs' => [['name' => 'class','value' => 'beautiful_list'],
                          ['name' => 'id',   'value' => 'unique_list']],
              'text'  => ['tag'  => ['name' => 'li'
                                     'text' => 'item 1'],
                          'tag'  => ['name' => 'li'
                                     'text' => 'item 2']]];

我希望这有点清楚吗?

这两个是等价的:

array()
[]