如何将含重复studentID的JSON数组转换为单条学生记录格式?
First, let's address your exact requirement—even though the desired JSON structure has duplicate keys (which isn't valid standard JSON, as most parsers ignore or overwrite duplicate keys), here's how you can achieve it using PHP:
Solution for Exact Desired Output
The key challenge here is that PHP associative arrays don't allow duplicate keys, so we need to construct the JSON string manually to preserve those duplicates. Here's the code:
<?php // Your input JSON $inputJson = '[{"studentID":"1","subjectID":"2","marks":65}, {"studentID":"1","subjectID":"3","marks":75}, {"studentID":"2","subjectID":"2","marks":80}, {"studentID":"2","subjectID":"3","marks":82}, {"studentID":"3","subjectID":"2","marks":"82"}, {"studentID":"3","subjectID":"3","marks":"75"}]'; // Decode input to PHP array $inputArray = json_decode($inputJson, true); // Group entries by studentID $studentGroups = []; foreach ($inputArray as $entry) { $studentId = $entry['studentID']; if (!isset($studentGroups[$studentId])) { $studentGroups[$studentId] = []; } // Remove studentID from individual entries (we'll add it once per student) unset($entry['studentID']); $studentGroups[$studentId][] = $entry; } // Build each student's JSON object string manually $outputEntries = []; foreach ($studentGroups as $id => $subjects) { $jsonParts = ["\"studentID\":\"{$id}\""]; foreach ($subjects as $subject) { // Encode each subject entry to JSON, then strip the outer braces $subjectJson = json_encode($subject); $jsonParts[] = substr($subjectJson, 1, -1); } // Combine parts into a single object string $outputEntries[] = "{" . implode(",", $jsonParts) . "}"; } // Assemble the final JSON array $outputJson = "[" . implode(",", $outputEntries) . "]"; // Output the result echo $outputJson; ?>
This code will produce exactly the JSON structure you requested, including duplicate subjectID and marks keys.
Recommended Valid JSON Alternative
Since duplicate keys in JSON are non-standard and can cause parsing issues, a better practice is to structure each student's subjects as an array. This ensures valid, maintainable JSON:
<?php $inputJson = '[{"studentID":"1","subjectID":"2","marks":65}, {"studentID":"1","subjectID":"3","marks":75}, {"studentID":"2","subjectID":"2","marks":80}, {"studentID":"2","subjectID":"3","marks":82}, {"studentID":"3","subjectID":"2","marks":"82"}, {"studentID":"3","subjectID":"3","marks":"75"}]'; $inputArray = json_decode($inputJson, true); $studentData = []; foreach ($inputArray as $entry) { $studentId = $entry['studentID']; if (!isset($studentData[$studentId])) { $studentData[$studentId] = [ 'studentID' => $studentId, 'subjects' => [] ]; } // Add subject details to the subjects array $studentData[$studentId]['subjects'][] = [ 'subjectID' => $entry['subjectID'], 'marks' => $entry['marks'] ]; } // Convert to valid, formatted JSON $outputJson = json_encode(array_values($studentData), JSON_PRETTY_PRINT); echo $outputJson; ?>
This will produce clean, valid JSON like:
[ { "studentID": "1", "subjects": [ { "subjectID": "2", "marks": 65 }, { "subjectID": "3", "marks": 75 } ] }, { "studentID": "2", "subjects": [ { "subjectID": "2", "marks": 80 }, { "subjectID": "3", "marks": 82 } ] }, { "studentID": "3", "subjects": [ { "subjectID": "2", "marks": "82" }, { "subjectID": "3", "marks": "75" } ] } ]
This structure is far more reliable for parsing and maintaining data consistency.
内容的提问来源于stack exchange,提问作者Enayet Hossain

