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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:27:10+00:00 2026-05-26T01:27:10+00:00

I have a database table as follows: This returns all column titles in the

  • 0

I have a database table as follows:

couldn't be bothered with an image description, I guess

This returns all column titles in the pic, but the one’s that are most important are slug, and parent (not sure about id_button).

The array gets ordered automatically by id_button ASC, which really irks me. But, anyways, this is not important, as I need to order it completely different, or re-order it after the array is populated.

The array returns this, by order of id_button:

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    2 => array(
            'id_button' => 3,
            'parent' => 'google.com',
            'position' => 'after',
            'slug' => 'another_test',
    ),
    3 => array(
            'id_button' => 4,
            'parent' => 'testing'
            'position' => 'child_of',
            'slug' => 'google.com',
    )
);

I need to order it so that if a slug is found within any parent, than the slug that is in the parent needs to be loaded before the one that has it defined within the parent.

Its not important if it is directly before it. For example, you see testing is the first slug that gets returned, and yet the parent for this is the last slug (google.com). So as long as the slug row where the parent is defined gets ordered so that it is BEFORE the row that has the slug value in the parent column, everything is fine.

So in this situation, it can be reordered as any of these 3 ordered arrays below:

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    2 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    3 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    )
);

OR this…

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    2 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    3 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    )
);

OR even this…

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    2 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    ),
    3 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    )
);

All 3 of these ordered arrays will work because the array with the slug that matches the parent is before the array with the matching parent, and since the slug value, sub_test_1 doesn’t match any of the parent values this array order is unimportant, so that array can be located anywhere within the array.

How can I do this? I’m thinking of just looping through the array somehow and trying to determine if the slug is in any of the parents, and just do a reordering somehow…

In short, the slug needs to be ordered before the parent ONLY if there is a parent that matches a slug within the array. Otherwise, if no match is found, the order isn’t important.

  • 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-26T01:27:10+00:00Added an answer on May 26, 2026 at 1:27 am

    Ok, first let me thank you all for your detailed explanations. They are very intuitive. However, I found another way, can you guys let me know if you spot anything wrong with this method here please?

    Click here to see a Demo of this working!

    $temp_buttons = array();
    foreach($new_menu_buttons as $buttons)
        $temp_buttons[$buttons['parent']] = $buttons['slug'];
    
    
    dp_sortArray($new_menu_buttons, $temp_buttons, 'slug');
    
    // The $new_menu_buttons array is now sorted correctly!  Let's check it...
    var_dump($new_menu_buttons);
    
    function dp_sortArray(&$new_menu_buttons, $sortArray, $sort)
    {
        $new_array = array();
        $temp = array();
        foreach ($new_menu_buttons as $key => $menuitem)
        {
            if (isset($sortArray[$menuitem[$sort]]))
            {
                $new_array[] = $menuitem;
    
                $temp[$menuitem['parent']] = $menuitem['slug'];
                unset($new_menu_buttons[$key]);
            }   
        }
    
        $ordered = array();
    
        if (!empty($new_array))
        {
            foreach ($new_array as $key => $menuitem)
            {
                if (isset($temp[$menuitem[$sort]]))
                {
                    $ordered[] = $menuitem;
                    unset($new_array[$key]);
                }
            }
        }
        else
        {
            $new_menu_buttons = $new_menu_buttons;
            return;
        }
    
        $new_menu_buttons = array_merge($ordered, $new_array, $new_menu_buttons);
    }
    

    Seems to work in all instances that I tested, but ofcourse, their could be a flaw in it somewhere. What do you all think of this?

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

Sidebar

Related Questions

i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table and one of the fields (not the primary key)
I have a database table that i want to allow my friends to update.
I have a database table on a development server that is now fully populated
I have a database table called Posts which stores all the information regarding an
I have a database table with a large number of rows and one numeric
I have a Database table that I want to display in a DataGridView. However,
I'm after a model-based database class for PHP/MySQL. But it must have one particular
I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I have a database table named call with columns call_time, location, emergency_type and there

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.