
以下是一个使用递归函数 breadcrumb() 从数组生成面包屑的示例代码:
function breadcrumb($array) {
$output = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach (breadcrumb($value) as $breadcrumb) {
$output[] = $key . ' > ' . $breadcrumb;
}
} else {
$output[] = $key;
}
}
return $output;
}代码解释:
示例用法:
假设我们有以下数组:
$array = [
'note' => [
'to' => [
'user' => [
'name' => 'First User'
],
'abc' => 123,
'lmn' => 4582
],
'from' => 'Jani',
'heading' => 'Reminder',
'body' => [
'abc' => 123
]
]
];
$breadcrumbs = breadcrumb($array);
print_r($breadcrumbs);输出结果:
Array
(
[0] => note > to > user > name
[1] => note > to > abc
[2] => note > to > lmn
[3] => note > from
[4] => note > heading
[5] => note > body > abc
)通过递归函数,我们可以方便地从多维数组生成面包屑导航。这种方法简单易懂,适用于大多数情况。在处理复杂的数组结构时,需要注意性能问题,并根据实际需求进行优化。
以上就是如何从数组生成面包屑导航?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号