php json_encode formatting results?

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.

Answers


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"}]}]

Need Your Help

Getting an Ace value to represent a 1 or a 11

console-application blackjack

I'm playing with some code for a blackjack program but I'm having trouble finding a way to make the Ace be a 1 or an 11, like in real blackjack. This isn't homework. I'm just getting back into

DatePicker type for export to SQL Azure

c# windows-phone-7 datetimepicker

I'm trying to take the date selection from the DatePicker (selection of which is put back into the TextBox on screen) and then save this to my SQL Azure database, where the type is defined as date.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.