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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:56:14+00:00 2026-06-16T13:56:14+00:00

I have 2 arrays of unknown length/depth that look something like this [1] =>

  • 0

I have 2 arrays of unknown length/depth that look something like this

[1] => Hand Tools
[2] => Power Tools
[4] =>  ╚═►Outdoor Tools
[6] =>    ╚═►dvjdg
[5] =>  ╚═►Indoor Tools
[7] =>    ╚═►blaha
[8] =>    ╚═►blahb
[3] => Garden Tools

and

[1] => 0
[2] => 0
[4] => 1
[6] => 2
[5] => 1
[7] => 2
[8] => 2
[3] => 0

Both arrays use a category id as the array index and the second array contains the depth of each category. The first array is used to generate a HTML <select> input but I now need to alphabetize this array while maintaining the proper category hierarchy. So I would need to get an output array like this.

[3] => Garden Tools
[1] => Hand Tools
[2] => Power Tools
[5] =>  ╚═►Indoor Tools
[7] =>    ╚═►blaha
[8] =>    ╚═►blahb
[4] =>  ╚═►Outdoor Tools
[6] =>    ╚═►dvjdg

How can I accomplish this?

  • 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-16T13:56:15+00:00Added an answer on June 16, 2026 at 1:56 pm

    Considering that $list is your array, you can not easily sort it because each “sub”-element does not carry the information (text) of it’s parent. So first thing is to add that information:

    1. Obtain the level.
    2. Set the current value for that level.
    3. Remove higher levels.
    4. Merge together all levels with a character that is not used in texts.

    And you’ve got the sortkey. Do this for all list entries and you’ve got all sortkeys:

    $sortkeys = [];
    $levels   = [];
    foreach ($list as $index => $entry) {
        $level            = strspn($entry, ' ');
        $levels[$level]   = $entry;
        $sortkey          = implode('|', array_slice($levels, 0, $level + 1));
        $sortkeys[$index] = $sortkey;
    }
    

    This example simplifies to obtain the level information. I have counted one space per level. You might have different and use a different function than strspn for that. Most likely you will use your second array for that. I was too lazy to type it, so I did it with the function. Your might just be:

    $level = $category_levels[$index];
    

    Now you can sort the $list array based on the $sortkeys array. That can be done with PHPs’ array_multisort function. Because that function would re-number numerical keys, we sort the keys as well and then combine after the sorting to preserve them:

    $keys = array_keys($list);
    array_multisort($sortkeys, SORT_ASC, $list, $keys);
    
    print_r(array_combine($keys, $list));
    

    Which gives you:

    Array
    (
        [3] => Garden Tools
        [1] => Hand Tools
        [2] => Power Tools
        [5] =>  ╚═►Indoor Tools
        [7] =>   ╚═►blaha
        [8] =>   ╚═►blahb
        [4] =>  ╚═►Outdoor Tools
        [6] =>   ╚═►dvjdg
    )
    

    Watch the full example running: Demo

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

Sidebar

Related Questions

I have arrays like this. [11] => Array ( [transactioncurrencyid] => Array ( [!name]
I have two byte[] arrays which are of unknown length and I simply want
I have an array that after being sorted appears like this: var arr =
Say you have an array, data, of unknown length. Is there a shorter method
-In my c code I have a struct which contains many unknown sized arrays
guys i have arrays in which i have to match this kind of text
Arrays have a "length" property by default. Can I add custom properties to them?
I have two arrays, @names and @employees , that are filled with strings that
If I have an array (of unknown length until runtime), is there a way
Say I have a json object with nested arrays to unknown depths. I want

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.