如何根据PHP中的某个键重新排列JSON对象?

时间:2018-03-27 02:41:21

标签: php json

让我们说我有一个像这样的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"
    }]
 }

正如您所看到的,有3个sectionId,所以我想根据sectionId重新排列 { "result": [{ "1": { "questionId": "1", "questionText": "Foo....", "questionType": "R" } } }, { "2": ..... }, { "3": .... }] } ,如下所示:

speech

我的PHP知识非常有限。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

获取每个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);

显示

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