Hallo allemaal,
Ik heb een multidimensional array met daarin telkens de keys: form, date, subform, question en answer.
Nu wil ik de formulieren groeperen op datum en alle subformulieren met bijbehorende vragen en antwoorden daarin samenvoegen.
Ik heb dus:
<?php
$answers =
[
[
'form' => 'form 1',
'date' => '30-01-2016',
'subform' => 'subform 1',
'question' => 'question 1',
'answer' => 'no'
],
[
'form' => 'form 1',
'date' => '30-01-2016',
'subform' => 'subform 1',
'question' => 'question 2',
'answer' => 'yes'
],
[
'form' => 'form 1',
'date' => '31-01-2016',
'subform' => 'subform 1',
'question' => 'question 1',
'answer' => 'yes'
],
[
'form' => 'form 1',
'date' => '31-01-2016',
'subform' => 'subform 1',
'question' => 'question 2',
'answer' => 'yes'
],
[
'form' => 'form 2',
'date' => '31-01-2016',
'subform' => 'subform 2',
'question' => 'question 3',
'answer' => 'no'
],
[
'form' => 'form 2',
'date' => '31-01-2016',
'subform' => 'subform 3',
'question' => 'question 4',
'answer' => 'no'
],
];
?>
en daaruit zou ik de volgende array willen hebben:
<?php
$all =
[
[
'naam' => 'form 1',
'date' => '30-01-2016',
'subforms' =>
[
[
'naam' => 'subform 1',
'questions' =>
[
[
'question' => 'question 1',
'answer' => 'no'
],
[
'question' => 'question 2',
'answer' => 'yes'
]
]
]
]
],
[
'naam' => 'form 1',
'date' => '31-01-2016',
'subforms' =>
[
[
'naam' => 'subform 1',
'questions' =>
[
[
'question' => 'question 1',
'answer' => 'yes'
],
[
'question' => 'question 2',
'answer' => 'yes'
]
]
]
]
],
[
'naam' => 'form 2',
'date' => '31-01-2016',
'subforms' =>
[
[
'naam' => 'subform 2',
'questions' =>
[
[
'question' => 'question 3',
'answer' => 'no'
]
]
],
[
'naam' => 'subform 3',
'questions' =>
[
[
'question' => 'question 4',
'answer' => 'no'
]
]
]
]
]
];
?>
Nu heb ik natuurlijk wel al het één en ander geprobeerd voordat ik hier mijn vraag kom stellen (zonder de check op datum), maar mijn array kunsten zijn nog vrij beperkt.
In zijn geheel ben ik tot het volgende gekomen:
<?php
$answers =
[
[
'form' => 'form 1',
'date' => '30-01-2016',
'subform' => 'subform 1',
'question' => 'question 1',
'answer' => 'no'
],
[
'form' => 'form 1',
'date' => '30-01-2016',
'subform' => 'subform 1',
'question' => 'question 2',
'answer' => 'yes'
],
[
'form' => 'form 1',
'date' => '31-01-2016',
'subform' => 'subform 1',
'question' => 'question 1',
'answer' => 'yes'
],
[
'form' => 'form 1',
'date' => '31-01-2016',
'subform' => 'subform 1',
'question' => 'question 2',
'answer' => 'yes'
],
[
'form' => 'form 2',
'date' => '31-01-2016',
'subform' => 'subform 2',
'question' => 'question 3',
'answer' => 'no'
],
[
'form' => 'form 2',
'date' => '31-01-2016',
'subform' => 'subform 3',
'question' => 'question 4',
'answer' => 'no'
],
];
$a=array();
// loop $answers array
foreach($answers as $k=>$v){
$allCurentForms=array();
foreach($a as $b){
array_push($allCurentForms, $b['naam']);
}
// als $v['form'] niet voorkomt in $allCurentForms
if(!in_array($v['form'], $allCurentForms)){
echo 'Form bestaat nog niet: <br>';
echo $v['form'].'<br>';
echo $v['subform'].'<br>';
$z =
[
'naam' => $v['form'],
'subforms' =>
[
[
'naam' => $v['subform'],
'questions' =>
[
[
'question' => $v['question'],
'answer' => $v['answer']
]
]
]
]
];
array_push($a, $z);
}else{
echo 'FORM BESTAAT AL!: <br>';
echo $v['form'].'<br>';
echo $v['subform'].'<br>';
$d=array();
foreach($a as $b){
var_dump($b['naam']);
foreach($b['subforms'] as $subform){
array_push($d, $subform['naam']);
}
}
if(!in_array($v['subform'], $d)){
$z =
[
'naam' => $v['subform'],
'questions' =>
[
[
'question' => $v['question'],
'answer' => $v['answer']
]
]
];
$currentFormKey;
foreach($a as $b){
$key = array_search($v['form'], $b); // $key = 2;
$currentFormKey = searchForId($v['form'], $a);
}
array_push($a[$currentFormKey]['subforms'], $z);
}
echo $v['form']. ' - zit al in de array <br>';
}
}
echo '<br /><br /><br /><br />';
foreach($a as $form){
echo '<h1>'.$form['naam']. '</h1>';
foreach($form['subforms'] as $subform){
echo '<h2>'.$subform['naam']. '</h2>';
foreach($subform['questions'] as $question){
echo $question['question'].'<br>';
echo $question['answer'].'<br>';
}
}
echo '<hr>';
}
echo '<pre>'. var_dump($a). '</pre>';
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['naam'] === $id) {
return $key;
}
}
return null;
}
?>
Ik vraag me af of ik hier niet al veel te moeilijk aan het doen ben, of dat ik op de goede weg zit.
Zoals eerder gezegd heb ik hier niet zoveel ervaring mee.
2.409 views