-1

Let's say I have a JSON object like this:

 {
    "result": [{
        "questionId": "1",
        "sectionId": "1",
        "questionText": "Foo bar blah blah",
        "questionType": "R"
    }, {
        "questionId": "2",
        "sectionId": "1",
        "questionText": "qoox foo",
        "questionType": "R"
    }, {
        "questionId": "3",
        "sectionId": "2",
        "questionText": "Hello world",
        "questionType": "D"
    }, {
        "questionId": "4",
        "sectionId": "2",
        "questionText": "asdasdasd",
        "questionType": "R"
    }, {
        "questionId": "5",
        "sectionId": "3",
        "questionText": "to be or not to be",
        "questionType": "D"
    }]
 }

As you can see, there are 3 sectionIds, so I'd like to rearrange that based on the sectionId, something like this:

 {
    "result": [{
            "1": {
                "questionId": "1",
                "questionText": "Foo....",
                "questionType": "R"
            }
        }
    },
    {
        "2": .....
    },
    {
        "3": ....
    }]
 }

My PHP knowledge is pretty limited. How can I do this?

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
anta40
  • 6,511
  • 7
  • 46
  • 73
  • 1
    Have you [**tried anything so far**](http://meta.stackoverflow.com/questions/261592)? Please update your question to show what you have already tried, showcasing a **specific** problem you are facing in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve). – Obsidian Age Mar 27 '18 at 02:45
  • Possible duplicate of [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – Andrew Mar 27 '18 at 02:53
  • You can not rearrange it by `sectionId` because there is a conflict among them, but if you would still like to arrange them using `sectionId` then be prepared to loose information. – Aniket Sahrawat Mar 27 '18 at 02:56

1 Answers1

1

Get all values for each sectionId:

$json = '{"result":[{"questionId":"1", "sectionId":"1","questionText":"Foo bar blah blah","questionType":"R"},{"questionId":"2", "sectionId":"1","questionText":"qoox foo","questionType":"R"},{"questionId":"3","sectionId":"2","questionText":"Hello world","questionType":"D"},{"questionId":"4","sectionId":"2","questionText":"asdasdasd","questionType":"R"},{"questionId":"5","sectionId":"3","questionText":"to be or not to be","questionType":"D"}]}';

$array = json_decode($json, true);

$sectionIds = array_values(
  array_unique(array_column($array['result'], 'sectionId'))
);

$jsonFinal = array_map(function($sectionId) use ($array) {
  return [
    $sectionId => array_values(array_filter($array['result'],
      function($result) use ($sectionId) {
        return $result['sectionId'] === $sectionId;
      }))
    ];
}, $sectionIds);

echo json_encode(['result' => $jsonFinal], JSON_PRETTY_PRINT);

Show

{
    "result": [
        {
            "1": [
                {
                    "questionId": "1",
                    "sectionId": "1",
                    "questionText": "Foo bar blah blah",
                    "questionType": "R"
                },
                {
                    "questionId": "2",
                    "sectionId": "1",
                    "questionText": "qoox foo",
                    "questionType": "R"
                }
            ]
        },
        {
            "2": [
                {
                    "questionId": "3",
                    "sectionId": "2",
                    "questionText": "Hello world",
                    "questionType": "D"
                },
                {
                    "questionId": "4",
                    "sectionId": "2",
                    "questionText": "asdasdasd",
                    "questionType": "R"
                }
            ]
        },
        {
            "3": [
                {
                    "questionId": "5",
                    "sectionId": "3",
                    "questionText": "to be or not to be",
                    "questionType": "D"
                }
            ]
        }
    ]
}
kip
  • 1,120
  • 7
  • 11