Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8409321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:52:34+00:00 2026-06-09T23:52:34+00:00

I’m trying to create a plugin like script for adding to a menu array….

  • 0

I’m trying to create a “plugin” like script for adding to a “menu array”…. I’ll go straight in..

Lets say I initiate a nav item like so:

$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
));

starts off by creating a new section and the getting the instance of.

We are then setting a “role” in which we will test against to see if the user is authenticated as being verified to view.

The set nav item simply stores the array. identifier is a reference to the item ( its for when we want to add sub items), all standard apart from “position” which states where it is to sit in the nav i.e. NULL is top level, array(‘topnav’,’subnav’) will be topnav->subnav->dashboard.

as a test, this could be stored as follows:

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

    )

)

my question being how I turn that into the following structure:

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

pulling my hair out over this one, any help would be very much appreciated.

regards

I currently have

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

EDIT

Imagine this is the array given (it can be in any order)

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
            )

    )

)

Which needs to be formatted as:

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] =>
                        )
                )
        )
) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:52:35+00:00Added an answer on June 9, 2026 at 11:52 pm

    Here’s my take on the problem, solved with recursion.

    You can use multiple positions (i guess this is why it’s an array), it will ignore missing positions if at least on of the positions is found, but will complain if every position is missing.

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

    In it’s current form it doesn’t remove the identifier or position keys from the node arrays once they are placed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.