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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:11:56+00:00 2026-06-11T05:11:56+00:00

I have: $all = array( array(‘id’=>1, ‘cat’=>’Main’,’type’=>’Name0′), array(‘id’=>2, ‘cat’=>’Main’,’type’=>’Name1′), array(‘id’=>3, ‘cat’=>’Main’,’type’=>’Name3′), array(‘id’=>4, ‘cat’=>’Main’,’type’=>’Name4′), array(‘id’=>5,

  • 0

I have:

$all = array(
    array('id'=>1, 'cat'=>'Main','type'=>'Name0'),
    array('id'=>2, 'cat'=>'Main','type'=>'Name1'),
    array('id'=>3, 'cat'=>'Main','type'=>'Name3'),
    array('id'=>4, 'cat'=>'Main','type'=>'Name4'),
    array('id'=>5, 'cat'=>'Secondary','type'=>'Name5'),
    array('id'=>6, 'cat'=>'Secondary','type'=>'Name6'),
    array('id'=>7, 'cat'=>'Secondary','type'=>'Name7'),
    array('id'=>8, 'cat'=>'Other','type'=>'Name8'),
    array('id'=>9, 'cat'=>'Other','type'=>'Name9'),
    array('id'=>10, 'cat'=>'Other','type'=>'Name10'),
    array('id'=>11, 'cat'=>'Other','type'=>'Name11'),
);

$result = array();
    foreach($all as $array){
    $result[$array['cat']][] = array('id'=>$array['id'],'type'=>$array['type']);
}

$json_type = json_encode($result);

Which returns:

{"Main":[{"id":"1","type":"name1"},{"id":"2","type":"name2"},{"id":"3","type":"name3"},{"id":"4","type":"name4"}],"Secondary":[{"id":"5","type":"name5"},{"id":"6","type":"name6"},{"id":"7","type":"name7"}],"Other":[{"id":"8","type":"name8"},{"id":"9","type":"name9"},{"id":"10","type":"name10"},{"id":"11","type":"name11"}]}

But I need it to return as:

[
{
    "text": "Main",
    "children": [
        {
            "id": "1",
            "text": "name1"
        },
        {
            "id": "2",
            "text": "name2"
        },
        {
            "id": "3",
            "text": "name3"
        },
        {
            "id": "4",
            "text": "name4"
        }
    ]
},
{
    "text": "Secondary",
    "children": [
        {
            "id": "5",
            "text": "name5"
        },
        {
            "id": "6",
            "text": "name6"
        },
        {
            "id": "7",
            "text": "name7"
        }
    ]
},
{
    "text": "Other",
    "children": [
        {
            "id": "8",
            "text": "name8"
        },
        {
            "id": "9",
            "text": "name9"
        },
        {
            "id": "10",
            "text": "name10"
        },
        {
            "id": "11",
            "text": "name11"
        }
    ]
}

]

To work with the select2 JQuery plugin, I’m working with.
the ‘children’ name doesn’t matter, I think that’s just a placeholder so it gets parsed correctly. I’m not sure how I would go about it, I’ve been trying str_replace() but even that hasn’t been working out so great.

  • 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-11T05:11:58+00:00Added an answer on June 11, 2026 at 5:11 am

    I would to it in 2 loops. The first one to group results by category, the 2nd one to format it to fit your needs:

    $temp = array();
    foreach($all as $array){    
        if (!isset($temp[$array['cat']])) {
            $temp[$array['cat']] = array();
        }
    
        $temp[$array['cat']][] = array('id'=>$array['id'], 'type'=>$array['type']);
    }
    
    $result = array();
    foreach ($temp as $key=>$value) {
        $result[] = array('text'=>$key, 'children'=>$value);
    }
    
    echo json_encode($result);
    

    This produces the following output:

    [{"text":"Main","children":[{"id":1,"type":"Name0"},{"id":2,"type":"Name1"},{"id":3,"type":"Name3"},{"id":4,"type":"Name4"}]},{"text":"Secondary","children":[{"id":5,"type":"Name5"},{"id":6,"type":"Name6"},{"id":7,"type":"Name7"}]},{"text":"Other","children":[{"id":8,"type":"Name8"},{"id":9,"type":"Name9"},{"id":10,"type":"Name10"},{"id":11,"type":"Name11"}]}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal is to have an array where all strings are of length n.
I have an array of variables all containing text. I want to select only
I have an array of values all well within the range 0 - 63,
I have an array in php that contains all the lines of a text
I have an array of array like below with all numeric values. I want
I have an array of strings: @array I want to concatenate all strings beginning
I have an array returned by the function Search.all : => [#<Search id: 7,
I have an array of views, and I need to hide them all, so
I have a an array of observable collections and I want to display all
I have all my annotations(as nsdictionnaries) in an array , and the users add

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.