Reputation:
There is a sorted list of questions without classification and I want to sort this list of questions. Because the system of the school works like this. I tried to do this, but I could only do this because I had little PHP knowledge.
In the JSON file, the first part is the question, followed by 4 options and the question begins again.
The amount of questions does not change. Always 4.
Currently JSON file:
$jsonData = '["questionText1","option1","option2","option3","option4","questionText2","option1","option2","option3","option4","questionText3","..."]'; // x150 text
My code:
$jsonParse = json_decode($jsonData, true);
foreach ($jsonParse as $json) {
$cikis[] = array('q' => $json[0]);
}
var_dump($cikis);
Goal:
$jsonData = [
{'q':'questionText','o1':'option1','o2':'option2','o3':'option3','o4':'option4'},
{'q':'questionText','o1':'option1','o2':'option2','o3':'option3','o4':'option4'}
]
I looked for a question like this but couldn't find it. What do I have to do to classify the question and question options separately? like the example above.
Upvotes: 0
Views: 149
Reputation: 4889
You can simply use array_chunk
to split your source data into chunks of five items, since each grouping always consists of 1 question + 4 options.
$chunks = array_chunk($jsonParse, 5);
This will give you an array of five-item chunks, with each chunk containing the question as the first item [0]
, and the options as items [1]...[4]
. If you want to turn them into associated arrays, you can do that for example using array_map
and array_combine
:
$namedChunks = array_map(function($chunk) {
return array_combine(['q', 'o1', 'o2', 'o3', 'o4'], $chunk);
}, $chunks);
This will give you an array with named keys, which you can json_encode($namedChunks)
for the output you're looking for. (Your example isn't valid JSON, though!)
A foreach loop and a "manual" building of the associated arrays will also work, and probably will be marginally faster. I'm in the habit of using straight-up loops in code where performance adds up (either frequently called, or with massive data), and using the more elegant array_*
functions in favor of more explicit and readable code, where raw performance is less consequential.
Upvotes: 1
Reputation:
You can use array_chunk() to split an array into samesized chunks:
<?php
$numberOfElementsPerChunk = 5; // 1 question + 4 options
$jsonData = '["questionText1","option1","option2","option3","option4","questionText2","option1-2","option2-2","option3-2","option4-2","questionText3","option1-3","option2-3","option3-3","option4-3","questionText4","option1-4","option2-4","option3-4","option4-4"]';
$jsonParse = json_decode($jsonData, true);
$chunks = array_chunk($jsonParse, $numberOfElementsPerChunk);
$result = [];
foreach ($chunks as $question) {
$set = [
'q' => $question[0],
'o1' => $question[1],
'o2' => $question[2],
'o3' => $question[3],
'o4' => $question[4],
];
$result[] = $set;
}
print_r(json_encode($result));
Output:
[{"q":"questionText1","o1":"option1","o2":"option2","o3":"option3","o4":"option4"},{"q":"questionText2","o1":"option1-2","o2":"option2-2","o3":"option3-2","o4":"option4-2"},{"q":"questionText3","o1":"option1-3","o2":"option2-3","o3":"option3-3","o4":"option4-3"},{"q":"questionText4","o1":"option1-4","o2":"option2-4","o3":"option3-4","o4":"option4-4"}]
Upvotes: 1