【发布时间】:2016-03-25 20:28:14
【问题描述】:
我正在尝试为我的网站导航行创建一个节点树。
这是一个带有一些默认键的示例父节点。
$allRows['inbox'] = [
"name" => 'inbox',
"icon" => 'inbox',
"link" => 'inbox',
"badge" => [
'active' => true,
'color' => 'yellow',
'text' => '14',
'position' => 'right',
],
];
这是一个带有一些子节点的父节点的示例。
$allRows['tables'] = [
"name" => 'tables.main',
"icon" => 'table',
"index" => [
[
'name' => 'tables.normal',
'link' => 'tables/normal',
],
[
'name' => 'tables.data-tables',
'link' => 'tables/data-tables',
'badge' => [
'active' => true,
'color' => 'green',
'text' => 'v1.10',
'position' => 'right',
],
],
[
'name' => 'tables.jquery-grid',
'link' => 'tables/jquery-grid',
],
],
];
我希望我的所有节点都适合这个默认结构。
$defaults = [
"name" => '',
"icon" => '',
"icon_color" => '',
"link" => '#',
"external" => false,
"badge" => [
'active' => false,
'color' => '',
'text' => '',
'position' => '',
],
"index" => [],
];
在我的导航类中,我像上面一样声明了$allRows。
我尝试通过作为引用传递 mergeWithDefaults($allRows) 方法来合并默认值,但无法完成我想要的。
public function mergeWithDefaults(&$navRows)
{
foreach ($navRows as &$navRow) {
$navRow = array_merge($this->defaults, $navRow);
if (! $this->isLeaf($navRow)) {
$navRow = $this->mergeWithDefaults($navRow['index']);
}
}
}
private function isLeaf($navRow)
{
return empty($navRow['index']);
}
为什么我得到空值。
array:11 [▼
"dashboard" => array:7 [▶]
"inbox" => array:7 [▶]
"graphs" => null
"tables" => null
"forms" => null
"ui-elements" => null
"calendar" => array:7 [▶]
"widgets" => array:7 [▶]
"app-views" => null
"gmap-skins" => array:7 [▶]
"miscellaneous" => null
]
我错过了什么?
【问题讨论】:
-
什么是 $this->defaults ?
-
我在粘贴时编辑了一些代码,引用了我上面提到的 $defaults 变量。
-
您想将 $allRows 转换为 $default 结构正确吗??
-
我希望每一行都有默认值。
标签: php arrays multidimensional-array nodes