我在php中有一个数组,在执行print_r($ breadcrumb)
时看起来像这样Array (
[url] => /category/
[text] => Category
)
Array (
[url] => /category/subcategory/
[text] => Subcategory
)
Array (
[url] => /category/subcategory/subsubcategory/
[text] => Subsubcategory
)
我希望这个痕迹显示为
Category > Subcategory > Subsubcategory
根据Google docs,HTML应如下所示:
<div xmlns:v="http://rdf.data-vocabulary.org/#">
<span typeof="v:Breadcrumb">
<a href="/category/" rel="v:url" property="v:title">
Category
</a> ›
<span rel="v:child">
<span typeof="v:Breadcrumb">
<a href="/category/subcategory/" rel="v:url" property="v:title">
Subcategory
</a> ›
<span rel="v:child">
<span typeof="v:Breadcrumb">
<a href="/category/subcategory/subsubcategory/" rel="v:url" property="v:title">
Subsubcategory
</a> ›
</span>
</span>
</span>
</span>
</span>
</div>
如何从PHP数组转到HTML?
我查看了other answers,但它们在我的方案中不起作用
答案 0 :(得分:1)
以下是我将如何做到这一点:
PHP:
<?php
$breadcrumb = array(
array(
'url' => '/category/',
'text' => 'Category'
),
array(
'url' => '/category/subcategory/',
'text' => 'Subcategory'
),
array(
'url' => '/category/subcategory/subcategory',
'text' => 'Subsubcategory'
)
);
$children = count($breadcrumb) - 1;
?>
显示标记:
<div xmlns:v="http://rdf.data-vocabulary.org/#">
<span typeof="v:Breadcrumb">
<?php for ($i = 0; $i < count($breadcrumb); $i++) : ?>
<?php if ($i == 0) : ?>
<a href="<?php echo $breadcrumb[0]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[0]['text']; ?></a> ›
<?php else : ?>
<span rel="v:child">
<span typeof="v:Breadcrumb">
<a href="<?php echo $breadcrumb[$i]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[$i]['text']; ?></a> ›
<?php endif; ?>
<?php endfor; ?>
<?php echo str_repeat("</span>\r\n</span>\r\n", $children); ?>
</span>
</div>
如果需要,此方法还允许更深的子类别。
希望有所帮助
更新了显示代码:
<div xmlns:v="http://rdf.data-vocabulary.org/#">
<span typeof="v:Breadcrumb">
<?php for ($i = 0; $i < count($breadcrumb); $i++) : ?>
<?php if ($i == 0) : ?>
<a href="<?php echo $breadcrumb[0]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[0]['text']; ?></a> ›
<?php else : ?>
<span rel="v:child">
<span typeof="v:Breadcrumb">
<a href="<?php echo $breadcrumb[$i]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[$i]['text']; ?></a><?php if ($i < $children):?> › <?php endif; ?>
<?php endif; ?>
<?php endfor; ?>
<?php echo str_repeat("</span>\r\n</span>\r\n", $children); ?>
</span>
</div>
上面的代码修剪了最后一个&gt;最后的符号:)