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 7627079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:24:42+00:00 2026-05-31T05:24:42+00:00

I have an array representing a menu and for all intents and purposes, it

  • 0

I have an array representing a menu and for all intents and purposes, it can be assumed to be infinitely nested.

The structure looks like this

array(
  0 => array(
         'label' => 'Item1',
         'link' => 'http://google.com',
         'children' => null
  )

  1 => array(
         'label' => 'Item2',
         'link' => 'http://google.com',
         'children' => array( 3 => array(
                                     'label' => 'SubmenuItem1',
                                     'link' => 'http://www.yahoo.com',
                                     'children' => null
                        )
          )
  )

  2 => array(
         'label' => 'Item3',
         'link' => 'http://google.com',
         'children' => null
  )
)

What I would like to do is to perform a comparison operation against all the links in the structure. Once the comparison returns true, we should perform some operation on that element and then stop the process.

However, the catch is that the comparison needs to start from the longest link in the structure and then move towards the shortest link. If there are multiple links with the same length, the first visited one would be used.

My initial thought was to do the following:

  • Walkthrough the whole array and harvest all the links and save it to an associative array using the key as the key and the link as the content:

    array(
    0 => 'http://google.com',
    1 => 'http://google.com',
    2 => 'http://google.com',
    3 => 'http://yahoo.com',
    )

  • Then sort the array by the length of its content.

  • Do the comparison operation and note the key of the element.

  • Go back to the original array and search for the noted key, and then do something to that element.

Is this the best way to accomplish this? Are there more efficient ways of doing this?

I am coding this in PHP, but this question is intended to be platform agnostic.

  • 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-05-31T05:24:43+00:00Added an answer on May 31, 2026 at 5:24 am

    Your solution sounds OK. I would however save the specific indexes of the links while gathering them in step 1, so that you don’t have to do the entire search again in step 4, but rather access the entry directly.

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

Sidebar

Related Questions

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
An array is defined of assumed elements like I have array like String[] strArray
Have an array of chars like char members[255]. How can I empty it completely
I have Hash like this, representing a data tree hash = { 'key1' =>
I have a menu structure (Drupal) that contains elements representing a menu link. If
I have an array of ushorts, with each ushort representing a 12-bit word. This
I have a 2D array representing a tree in this format: [[Food, 0], [Dairy,
I have an array of bytes representing an image in Windows BMP format and
have an array of values [1,2,4] representing the values of a multi-select box, how
I have a byte array representing a picture. I want to present the picture

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.