Php 从数组重新创建html页面(2.0)
各位早上好,, 昨天我问了一个问题来解决这个问题,但数组有缺陷。 现在数组似乎已修复,但我在创建页面时遇到了问题。 这是固定数组Php 从数组重新创建html页面(2.0),php,html,arrays,foreach,tags,Php,Html,Arrays,Foreach,Tags,各位早上好,, 昨天我问了一个问题来解决这个问题,但数组有缺陷。 现在数组似乎已修复,但我在创建页面时遇到了问题。 这是固定数组 'tag' => array( 'tagName' => 'section', 'tagAttributes' => array( 'class' => 'main full', ), 'tagContents' => array( 'tag' => array(
'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;
foreach($ministitestructurearray作为$section=>$sectionStructure){
$html='';
}
如果(isset($contents['tagContents'])){
$html.=findAllContents($contents['tagContents'],$html);
}
}
$html.='';
}
函数findAllContents($tagContents,$tmpHtml){
foreach($tagContents作为$tag=>$tagValue){
$tmpHtml='';
}
返回$tmpHtml;
}
好多了,谢谢
现在仍然是$html问题。
我一直在考虑在代码的开头打开一个html标记,比如$html=
,然后在结尾关闭html标记和body标记,但我尝试了,但没有成功
编辑2:
好的,我想已经完成了。我只在开头添加了一个this
$html='
。您必须在每个循环的tagAttributes
之前打开标记,然后关闭。如果不将tagAttributes键添加到数组中,也会出现问题,因此标记本身将被忽略
为findAllContents()
修改foreach循环的示例:
foreach($tagContents作为$tag=>$tagValue){
$tmpHtml.='';
}
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;
}
foreach ($tagContents as $tag => $tagValue) {
$tmpHtml .= '<' . $tagValue['tagName']
if (isset($tagValue['tagAttributes'])) {
foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
$tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
}
}
$tmpHtml .= '></' . $tagValue['tagName'] . '>';
}