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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:55:55+00:00 2026-06-13T03:55:55+00:00

I have the following array structure: Array ( [0] => Array ( [id] =>

  • 0

I have the following array structure:

Array
(
    [0] => Array
        (
            [id] => 83
            [parent_id] => 0
            [title] => Questionnaire one
        )

    [1] => Array
        (
            [id] => 84
            [parent_id] => 0
            [title] => Questionnaire two
        )

    [2] => Array
        (
            [id] => 85
            [parent_id] => 83
            [title] => Questionnaire three
        )

)

I want to re-structure the array so child items are listed under their parents. For example:

Array
(
    [0] => Array
        (
            [id] => 83
            [parent_id] => 0
            [title] => Questionnaire one
        )

    [1] => Array
        (
            [id] => 85
            [parent_id] => 83
            [title] => Questionnaire three
        )

    [2] => Array
        (
            [id] => 84
            [parent_id] => 0
            [title] => Questionnaire two
        )
)

I’ve searched previous questions but found none of them actually achieve the above.

Can someone please help me with this?

Thanks

  • 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-13T03:55:56+00:00Added an answer on June 13, 2026 at 3:55 am

    You can try

    $array = Array(
            "0" => Array("id" => 83,"parent_id" => 0,"title" => "Questionnaire one"),
            "1" => Array("id" => 84,"parent_id" => 0,"title" => "Questionnaire two"),
            "2" => Array("id" => 85,"parent_id" => 83,"title" => "Questionnaire three"));
    
    $id = array_map(function ($item) {return $item["id"];}, $array);
    $parent = array_filter($array, function ($item){return $item['parent_id'] == 0;});
    $lists = array();
    
    foreach ($parent as $value)
    {
        $lists[] = $value ;
        $children = array_filter($array, function ($item) use($value) {return $item['parent_id'] == $value['id'];});
        foreach($children as $kids)
        {
            $lists[]  = $kids ;
        }
    }
    
    echo "<pre>";
    print_r($lists);
    

    Output

    Array
    (
        [0] => Array
            (
                [id] => 83
                [parent_id] => 0
                [title] => Questionnaire one
            )
    
        [1] => Array
            (
                [id] => 85
                [parent_id] => 83
                [title] => Questionnaire three
            )
    
        [2] => Array
            (
                [id] => 84
                [parent_id] => 0
                [title] => Questionnaire two
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array structure in MySQL. There could be no items or
I have the following array structure: Array ( [0] => Array ( [configuration_id] =>
I have the following JavaScript data structure. a[] is an array with text strings
I have taken following array; to fill-up gallery: private Integer[] mImageIds = { R.drawable.one,
I have the following array structure (linked list): struct str_pair { char ip [50]
I have an array called $all_countries following this structure: Array ( [0] => Array
i have the following array structure: | ID | CategoryName | ParentID _________________________________ 1
I have the following array structure: 0 => array 'all_sessions_available' => boolean true 'all_sessions_unavailable'
I have the following array structure: [[a], [b, c, [d, e]]] That could have
I have the following array with a structure within each array (see this jsfiddle

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.