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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:03:40+00:00 2026-06-12T01:03:40+00:00

This is an output array pushed from mysql query Array ( [0] => Array

  • 0

This is an output array pushed from mysql query

Array
(
    [0] => Array
        (
            [CategoryID] => 7
            [CategoryName] => Test it
            [Parent] => 2
            [Thumb] => 4
            [sort] => 0
            [MediaID] => 4
            [MediaTitle] => asd
            [MediaName] => 1349012691_4.jpg
            [MediaType] => image
            [MediaSize] => 369848
            [UploadTime] => 1349012691
        )

    [1] => Array
        (
            [CategoryID] => 8
            [CategoryName] => Test all
            [Parent] => 4
            [Thumb] => 5
            [sort] => 0
            [MediaID] => 5
            [MediaTitle] => asaas
            [MediaName] => 1349012728_9.jpg
            [MediaType] => image
            [MediaSize] => 416817
            [UploadTime] => 1349012728
        )

    [2] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [Thumb] => 0
            [sort] => 2
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [3] => Array
        (
            [CategoryID] => 2
            [CategoryName] => Test Sub
            [Parent] => 1
            [Thumb] => 0
            [sort] => 4
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [4] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 6
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [5] => Array
        (
            [CategoryID] => 1
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [6] => Array
        (
            [CategoryID] => 5
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 10
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [7] => Array
        (
            [CategoryID] => 6
            [CategoryName] => Test Remove
            [Parent] => 0
            [Thumb] => 0
            [sort] => 12
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

)

I need to sort this array so it show Parents Category First then child categories.

I need the output something like

Array
(

    [2] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [Thumb] => 0
            [sort] => 2
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )
        //sub category
            [1] => Array
            (
                [CategoryID] => 8
                [CategoryName] => Test all
                [Parent] => 4
                [Thumb] => 5
                [sort] => 0
                [MediaID] => 5
                [MediaTitle] => asaas
                [MediaName] => 1349012728_9.jpg
                [MediaType] => image
                [MediaSize] => 416817
                [UploadTime] => 1349012728
            )

    [3] => Array
        (
            [CategoryID] => 2
            [CategoryName] => Test Sub
            [Parent] => 1
            [Thumb] => 0
            [sort] => 4
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )
                //sub category
                [0] => Array
                (
                    [CategoryID] => 7
                    [CategoryName] => Test it
                    [Parent] => 2
                    [Thumb] => 4
                    [sort] => 0
                    [MediaID] => 4
                    [MediaTitle] => asd
                    [MediaName] => 1349012691_4.jpg
                    [MediaType] => image
                    [MediaSize] => 369848
                    [UploadTime] => 1349012691
                )

    [4] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 6
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [5] => Array
        (
            [CategoryID] => 1
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [6] => Array
        (
            [CategoryID] => 5
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 10
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [7] => Array
        (
            [CategoryID] => 6
            [CategoryName] => Test Remove
            [Parent] => 0
            [Thumb] => 0
            [sort] => 12
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

)

Can I do this with PHP?

  • 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-12T01:03:41+00:00Added an answer on June 12, 2026 at 1:03 am

    You can try

    # Short Version of your Array
    $oldList = Array(
        "0" => Array("CategoryID" => 7,"CategoryName" => "Test it","Parent" => 2),
        "1" => Array("CategoryID" => 8,"CategoryName" => "Test all","Parent" => 4),
        "2" => Array("CategoryID" => 4,"CategoryName" => "Test Image","Parent" => 0),
        "3" => Array("CategoryID" => 2,"CategoryName" => "Test Sub","Parent" => 1),
        "4" => Array("CategoryID" => 3,"CategoryName" => "Test Category","Parent" => 0),
        "5" => Array("CategoryID" => 1,"CategoryName" => "Test Category","Parent" => 0),
        "6" => Array("CategoryID" => 5,"CategoryName" => "Test Category","Parent" => 0),
        "7" => Array("CategoryID" => 6,"CategoryName" => "Test Remove","Parent" => 0)
    );
    
    echo "<pre>";
    print_r(__group($oldList));
    

    Function used

    function __group($oldList)
    {
        # Get Category Position
        $category = array_map(function($item) { return $item['CategoryID'];}  ,$oldList); 
    
        #Get Only Parent List and add to new list
        $newList = array_filter($oldList , function($item) {  return $item['Parent']  == 0 ; } );
    
        foreach($oldList as $key => $value)
        {
            if($value['Parent'] != 0) {
                # Mapt Chiled to parent using Parent ID & Category ID 
                $newList[array_search($value['Parent'],$category)]['child'][] = $value;
            }
        }
        return $newList ;
    }
    

    Output

    Array
    (
        [2] => Array
            (
                [CategoryID] => 4
                [CategoryName] => Test Image
                [Parent] => 0
                [child] => Array
                    (
                        [0] => Array
                            (
                                [CategoryID] => 8
                                [CategoryName] => Test all
                                [Parent] => 4
                            )
    
                    )
    
            )
    
        [4] => Array
            (
                [CategoryID] => 3
                [CategoryName] => Test Category
                [Parent] => 0
            )
    
        [5] => Array
            (
                [CategoryID] => 1
                [CategoryName] => Test Category
                [Parent] => 0
                [child] => Array
                    (
                        [0] => Array
                            (
                                [CategoryID] => 2
                                [CategoryName] => Test Sub
                                [Parent] => 1
                            )
    
                    )
    
            )
    
        [6] => Array
            (
                [CategoryID] => 5
                [CategoryName] => Test Category
                [Parent] => 0
            )
    
        [7] => Array
            (
                [CategoryID] => 6
                [CategoryName] => Test Remove
                [Parent] => 0
            )
    
        [3] => Array
            (
                [child] => Array
                    (
                        [0] => Array
                            (
                                [CategoryID] => 7
                                [CategoryName] => Test it
                                [Parent] => 2
                            )
    
                    )
    
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$jj_post is the array output debug via print_r() . This variable is an array
I have different php output in jQuery-based tabs. This output is formed from database
I am trying to debug a segfault, and I have this output from gdb:
So what's getting pushed to this array is dependant on a few radio boxes.
My console output looks like this: 2011-12-07 02:46:48.051 Calculator[13186:f803] Operand pushed to stack: 23
This is my array output Array ( [0] => Array ( [tweet_text] => Fedora
I have a php array output like this Array ( [DISEASE] => Array (
I need to get an array output as this [[1, 26], [3, 16], [3,
Why does this output a 0 byte file? <?php $jsonurl = http://do.convertapi.com/Web2Pdf/json/?curl=http://stackoverflow.com/; $json =
There is this output of objdump on some object file: $ objdump -h main.o

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.