Я пытаюсь сгенерировать спецификации c json для рубрик, для которых требуется только этот формат ниже.
Зачем мне это нужно? https://surveyjs.io/Examples/Library?id=questiontype-matrix-rubric&platform=jQuery&theme=bootstrap#content - js
Для опроса JS я создаю ячейки json, используя PHP
PHP вывод
Array
(
[Attendance] => Array
(
[0] => Array
(
[OUTSTANDING] => A
)
[1] => Array
(
[Satisfactory] => B
)
[2] => Array
(
[Needs Improvement] => C
)
[3] => Array
(
[Unacceptable] => D
)
)
[Punctuality] => Array
(
[0] => Array
(
[OUTSTANDING] => A
)
[1] => Array
(
[Satisfactory] => B
)
[2] => Array
(
[Needs Improvement] => C
)
[3] => Array
(
[Unacceptable] => D
)
)
)
Результат, который я пытался достичь и требовал
{
"Attendance": {
"OUTSTANDING": "A",
"Satisfactory": "B",
"Needs Improvement": "C",
"Unacceptable": "D"
},
"Punctuality": {
"OUTSTANDING": "A",
"Satisfactory": "B",
"Needs Improvement": "C",
"Unacceptable": "D"
}
}
Это то, что я пробовал до сих пор: -
$finalArr = [];
$columns = [];
$rows = [];
$cells = [];
$rubics = json_decode($model->rubric_json);
$points = json_decode($model->points_json);
$rubicsCols = json_decode($model->rubric_json);
if (!empty($rubicsCols[0])) {
$columns = array_filter($rubicsCols[0], 'strlen');
}
if (!empty($points[0])) {
$points = array_filter($points[0], 'strlen');
}
unset($rubics[0]);
if (!empty($rubics)) {
foreach ($rubics as $key => $data) {
$rows[] = [
'value' => $data[0],
'text' => $data[0]
];
}
}
if (!empty($rows)) {
foreach ($rows as $rowKey => $row) {
foreach ($columns as $key => $column) {
$cells[$row['text']][] =[
$column =>$rubics[$rowKey + 1][$key]
] ;
}
}
}
echo "<pre>";
print_r(json_encode($cells,JSON_PRETTY_PRINT));exit;
после json_encode ($ cells, JSON_PRETTY_PRINT) вывод: -
{
"Attendance": [
{
"OUTSTANDING": "Attends all of class.Never arrives late, leaves early, or is in and out of the class while it is in progress."
},
{
"Satisfactory": "Attends most of class.Rarely arrives late, leaves early, or is in and out of the class while it is in progress."
},
{
"Needs Improvement": "Sometimes absent.Sometimes arrives late, leaves early, or is in and out of the class while it is in progress."
},
{
"Unacceptable": "Often absent.Often arrives late, leaves early, or is in and out of the class while it is in progress."
}
],
"Punctuality": [
{
"OUTSTANDING": "Always ready to start when class starts at the beginning of the day and when class resumes after breaks."
},
{
"Satisfactory": "Usually ready to start when class starts at the beginning of the day and when class resumes after breaks."
},
{
"Needs Improvement": "Rarely ready to start when class starts at the beginning of the day and when class resumes after breaks."
},
{
"Unacceptable": "Almost never ready to start when class starts at the beginning of the day and when class resumes after breaks"
}
]
}