2

我正在尝试创建一个类似于脚本的“插件”,用于添加到“菜单数组”......我会直接进去......

假设我像这样启动一个导航项目:

$sections->add_section('dashboard');
$DashBoardSection = $sections->load_section('dashboard');
$DashBoardSection->set_role(NULL);
$DashBoardSection->set_nav_item(array(
    'identifier' => 'dashboard',
    'text' => 'Dashboard',
    'url' => NULL,
    'position' => NULL
));

首先创建一个新部分并获取其实例。

然后,我们将设置一个“角色”,我们将在其中进行测试,以查看用户是否经过身份验证,可以查看。

set nav 项只是存储数组。标识符是对项目的引用(它用于我们想要添加子项目时),除了“位置”之外的所有标准,它说明它在导航中的位置,即 NULL 是顶级,array('topnav','subnav ') 将是 topnav->subnav->dashboard。

作为测试,这可以存储如下:

Array
(
    [0] => Array
        (
            [identifier] => dashboard
            [text] => Dashboard
            [url] => 
            [position] => 
        )
[1] => Array
    (
        [identifier] => dashboard2
        [text] => Dashboard2
        [url] => 
        [position] => Array
            (
                [0] => dashboard
            )

    )

)

我的问题是我如何把它变成以下结构:

Array
(
    [0] => Array
    (
        [identifier] => dashboard
        [text] => Dashboard
        [url] => 
        [position] => 
        [children] => Array
            (
                [0] => Array
                    (
                        [identifier] => dashboard2
                        [text] => Dashboard2
                        [url] => 
                    )
            )
    )
)

把我的头发拉到这个上面,非常感谢任何帮助。

问候

我目前有

public function build_navigation( $role ){
    $role = (int)$role;
    $nav  = array();
    foreach( $this->sections as $section ) {
        if( $section->get_role() === NULL || $section->get_role() === $role ) {
            $nav_array = $section->get_nav();
            foreach( $nav_array as $key => $nav_item ) {
                if( $nav_item['position'] === NULL ) {
                    $nav[$nav_item['identifier']] = $nav_item;
                }elseif( is_array( $nav_item['position'] ) ){
                    #...#
                }
            }
        }
    }
    return $nav;
}

编辑

想象一下这是给定的数组(它可以是任何顺序)

Array
(
    [0] => Array
        (
            [identifier] => dashboard_child2
            [text] => Dashboard Child 2
            [url] => 
            [position] => Array
                (
                    [0] => dashboard
                )

        )

    [1] => Array
        (
            [identifier] => dashboard_child_child_1
            [text] => Dashboard Child Child 1
            [url] => 
            [position] => Array
                (
                    [0] => dashboard
                    [1] => dashboard_child1
                )

        )



[2] => Array
    (
        [identifier] => dashboard_child1
        [text] => Dashboard Child 1
        [url] => 
        [position] => Array
            (
                [0] => dashboard
            )

    )

[3] => Array
    (
        [identifier] => dashboard
        [text] => Dashboard
        [url] => 
        [position] => 
    )

[4] => Array
    (
        [identifier] => dashboard2
        [text] => Dashboard2
        [url] => 
        [position] => Array
            (
                [0] => dashboard
            )

    )

)

需要格式化为:

Array
(
    [dashboard] => Array
        (
            [text] => Dashboard
            [url] => 
            [children] => Array
                (
                    [dashboard_child2] => Array
                        (
                            [text] => Dashboard Child 2
                            [url] => 
                        )
                    [dashboard_child1] => Array
                        (
                            [text] => Dashboard Child 1
                            [url] => 
                            [children] => Array
                                (
                                    [dashboard_child_child_1] => Array
                                        (
                                            [text] => Dashboard Child Child 1
                                            [url] => 
                                        )
                                )
                        )
                    [dashboard2] => Array
                        (
                            [text] => Dashboard2
                            [url] =>
                        )
                )
        )
) 
4

2 回答 2

2

您需要一个将标识符映射到它的子数组的临时数组,以便您可以将其添加到那里。

如果我没看错,那么当您添加父级时,您已经有类似的东西了:

$nav[$nav_item['identifier']] = $nav_item;

编辑:添加父母需要一些谨慎,如评论中指出的那样:

$node = &$nav[$nav_item['identifier']];
$children = isset($node['children']) ? $node['children'] : array();
$node = $nav_item;
$node['children'] = $children;
unset($node);

只需添加到孩子然后:

foreach($nav_item['position'] as $identifier)
{
    $nav[$identifier]['children'][] = $nav_item;
}

您也可以使用对象,而不是数组,这样您就不会复制太多数据(或者您可以稍后更改它),但是,只是想,这不一定是解决您的问题所必需的,因此可能需要稍后再做。

于 2012-08-16T19:03:31.540 回答
2

这是我对这个问题的看法,通过递归解决。

您可以使用多个位置(我想这就是它是一个数组的原因),如果找到至少一个位置,它将忽略丢失的位置,但如果每个位置都丢失,它会抱怨。

function translate($in) {

    $out = array();

    // first pass, move root nodes to output
    foreach ($in as $k => $row) {
        if (!$row['position']) {
            $out[$row['identifier']] = $row;
            unset($in[$k]);
        }
    }

    // while we have input
    do {
        $elements_placed = 0;
        // step trough input
        foreach ($in as $row_index => $row) {
            foreach ($row['position'] as $pos) {
                // build context for the node placing
                $data = array(
                    'row' => $row,
                    'in'  => &$in,
                    'row_index' => $row_index,
                    'elements_placed' => &$elements_placed,
                    'pos' => $pos,
                );
                // kick of recursion
                walker($out, $data);
            }
        }
    } while ($elements_placed != 0);
    if (count($in)) {
        trigger_error("Error in user data, can't place every item");
    }
    return $out;
}

function walker(&$out, $data) {
    foreach ($out as &$row) {
        // it looks like a node array
        if (is_array($row) && isset($row['identifier'])) {
            // if it has children recurse in there too
            if (isset($row['children'])) {
                walker($row['children'], $data);
            }

            // it looks like a node array that we are looking for place the row
            if ($row['identifier'] == $data['pos']) {
                if (!isset($row['children'])) {
                    $row['children'] = array($data['row']['identifier'] => $data['row']);
                } else {
                    $row['children'][$data['row']['identifier']] = $data['row'];
                }
                // report back to the solver that we found a place
                ++$data['elements_placed'];
                // remove the row from the $in array
                unset($data['in'][$data['row_index']]);
            }
        }
    }
}

$in = array (
    array (
        'identifier' => 'secondlevelchild2',
        'text' => 'secondlevelchild2',
        'url' => '',
        'position' => array (
            'dashboard2',
        ),
    ),
    array (
        'identifier' => 'secondlevelchild',
        'text' => 'secondlevelchild',
        'url' => '',
        'position' => array (
            'dashboard2',
        ),
    ),
    array (
        'identifier' => 'dashboard',
        'text' => 'Dashboard',
        'url' => '',
        'position' => '',
    ),
    array (
        'identifier' => 'dashboard2',
        'text' => 'Dashboard2',
        'url' => '',
        'position' => array (
            'dashboard', 'home',
        ),
    ),
    array (
        'identifier' => 'thirdlevelchild',
        'text' => 'thirdlevelchild',
        'url' => '',
        'position' => array (
            'secondlevelchild2',
        ),
    ),
);

$out = translate($in);
var_export($out);

在它的当前形式中,一旦放置它们,它就不会从节点数组中删除identifier或键。position

于 2012-08-16T20:03:54.357 回答