1

Just building on top of my previous question here:

Rewriting a JSON string - grouping keys by value into a new array?

I am wondering how do I traverse the array given and resort it so that the output comes out something like this:

[{
"Name": "Title 1",
"Date": "2012-12-05",
"rows": [
    {
        "Subtitle": "Subtitle 1",
        "Count1": 566,
        "Count2": 105
    },
    {
        "Subtitle": "Subtitle 2",
        "Count1": 76,
        "Count2": 15
    }
    ]
}, 
{
"Name": "Title 2",
"Date": "2012-12-06",
"rows": [
    {
        "Count1": 66,
        "Count2": 5
    }...

where the original json array is grouped by "Name" column, and then sorted into multi-level structure like this (parent/multiple child json structure for properly building menu in our application)?

The original json data looks like this:

[{
"Name": "Title 1",
"Subtitle": "Subtitle 1",
"Count1": 556,
"Count2": 5,
"Date": "2012-12-05"
}, {
"Name": "Title 1",
"Subtitle": "Subtitle 2",
"Count1": 10,
"Count2": 100,
"Date": "2012-12-05"
}, {
"Name": "Title 3",
"Subtitle": "Subtitle 3",
"Count1": 798,
"Count2": 11,
"Date": "2012-12-04"
}...

and obviously I am looking for a solution in PHP, as I will read in the json data, process it and spit it out reformatted.

2
  • What does the original data look like? The data in the earlier question doesn't have subtitles. And more to the point: what have you tried? You seem to be using SO as a "write this for me" service, it's supposed to be "help me fix my code". Commented Dec 23, 2012 at 7:58
  • I've updated the original question with the sample json data for this... and what have I tried? Nothing yet as I can't even fathom in my head how I'd approach this to begin with... hence the question. If I tried something and it was close but didn't work exactly the way I needed it, I would've posted that as well. Commented Dec 23, 2012 at 12:05

2 Answers 2

2

Assuming associative arrays and sorting output by name:

$output = array();

for ($data as $row) {

    if (!isset($output[$row['Name']])) {
        $output[$row['Name']] = array(
            'Name' => $row['Name'],
            'Date' => $row['Date'],
            'rows' => array()
        );
    }

    $output[$row['Name']]['rows'][] = array(
        'Subtitle' => $row['Subtitle'],
        'Count 1'  => $row['Count 1'],
        'Count 2'  => $row['Count 2'] 
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was pretty close with what I had on my end... I just ended up using array_push at the very end instead to add a new child item, but was missing a piece of the puzzle from the code above. Thanks!
1
hash = {};
result = [];
for (var i = 0; i < data.length; i++) {
    var name = data[i].Name;
    if (!hash.hasOwnProperty(name)) {
        var newitem = { Name: name,
                        Date: data[i].Date,
                        rows: []
                      };
        result.push(newitem);
        hash[name] = newitem;
    }
    hash[name].rows.push({ Subtitle: data[i].Subtitle,
                           Count1: data[i].Count1,
                           Count2: data[i].Count2
                         });
}

2 Comments

I'd need this in PHP, as doing it in the front-end, given the data set I'll be working with, will cause way too much overhead in the browser... but thanks for that approach/solution.
Sorry, missed that. But the algorithm is the same, just change the syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.