【问题标题】:How can I make breadcrumbs from a array?如何从数组中制作面包屑?
【发布时间】:2021-12-12 12:26:45
【问题描述】:

这是示例数组

Array
    (
        [note] => Array
            (
                [to] => Array
                    (
                        [user] => Array
                            (
                                [name] => First User
                            )
    
                        [abc] => 123
                        [lmn] => 4582
                    )
    
                [from] => Jani
                [heading] => Reminder
                [body] => Array
                    (
                        [abc] => 123
                    )
    
            )
    
    )

我想要上述数组的以下输出。

注释 > 到 > 用户 > 姓名

note > to > abc

注意 > to > lmn

注意>来自

注意>标题

注意 > 正文 > abc

【问题讨论】:

  • 您可能需要考虑现有的包,例如 Laravel Breadcrumbs,因为它可能会提供您想要/需要的功能(例如链接到路由等)。

标签: php arrays laravel breadcrumbs


【解决方案1】:

一个简单的方法是通过递归函数:

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;
}

Try it online.

【讨论】:

  • 我正要粘贴相同的答案,但你打败了我。
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多