Anhand der Fragen und deren Antworten könntest Du Werte in ein Array schreiben.
Hatte vor kurzem etwas ähnliches gebraucht und das hier geschrieben: Das Array mit dem größten "Gewicht" finden
Wie die Funktion funktioniert steht im Post. Bei Fragen - einfach fragen.
Wie Du die Werte erstellst musst Du Dir aber selbst einfallen lassen ![]()
evtl hat jede Frage zB einen Wert. Bei DER richtigen Antwort -> volle Punkte, sonst weniger.
in etwa so(?):
EDIT: hier mal am Bsp:
PHP
/* prinzip:
$source = array(
'frage_1' => array(
'antwort_1' => 0,// 0-10 punkte
'antwort_2' => 0,// 0-10 punkte
'antwort_3' => 0,// 0-10 punkte
'antwort_4' => 0,// 0-10 punkte
'antwort_5' => 0,// 0-10 punkte
),
'frage_2' => array(
'antwort_1' => 0,// 0-10 punkte
'antwort_2' => 0,// 0-10 punkte
'antwort_3' => 0,// 0-10 punkte
'antwort_4' => 0,// 0-10 punkte
'antwort_5' => 0,// 0-10 punkte
),
...
...
);
*/
// erstellen von $source array (5 fragen, je 5 antworten) mit zufalligen antworten zum testen:
for($fragen = 1; $fragen <= 5; $fragen++){
for($antworten = 1; $antworten <= 5; $antworten++){
$source['f_' . $fragen]['antwort_' . $antworten] = rand(0, 10); // random punkte von 0-10
}
}
$r = array_weigth($source);
echo '
<pre>
source:
';
print_r($source);
echo '
ergebnis:
frage "' . $r . '" wurden (nach gewicht) am besten beantwortet
</pre>
';
function array_weigth(array $source, array $compare_keys=array()){
if(!function_exists('array_weigth_')){
function array_weigth_(array &$source, $source_key, $target_source_key, array &$compare_keys, $compare_key, $greater_before){
$current_source_val = $source[$source_key][$compare_keys[$compare_key]];
$current_target_val = $source[$target_source_key][$compare_keys[$compare_key]];
$greater = ($current_source_val > $current_target_val);
$greater_equal = ($current_source_val >= $current_target_val);
if(
($greater_equal === true or $greater_before === true)
and (
!isset($compare_keys[++$compare_key])
or array_weigth_(
$source, $source_key, $target_source_key, $compare_keys, $compare_key, $greater
) === true
)
){
return true;
}
return false;
}
}
if(count($compare_keys) === 0){
foreach($source as $array){
if(!is_array($array)){
return false;
}
$compare_keys = array_unique(array_merge($compare_keys, array_keys($array)));
}
if(count($compare_keys) === 0){
return false;
}
}
reset($source);
$target_source_key = key($source);
foreach(array_keys($source) as $source_key){
$target_source_key = (
array_weigth_($source, $source_key, $target_source_key, $compare_keys, 0, null) === true
) ? $source_key : $target_source_key;
}
return $target_source_key;
}
// bsp ausgabe:
/*
source:
Array
(
[f_1] => Array
(
[antwort_1] => 8
[antwort_2] => 8
[antwort_3] => 2
[antwort_4] => 3
[antwort_5] => 5
)
[f_2] => Array
(
[antwort_1] => 6
[antwort_2] => 9
[antwort_3] => 5
[antwort_4] => 7
[antwort_5] => 2
)
[f_3] => Array
(
[antwort_1] => 0
[antwort_2] => 5
[antwort_3] => 5
[antwort_4] => 0
[antwort_5] => 6
)
[f_4] => Array
(
[antwort_1] => 6
[antwort_2] => 1
[antwort_3] => 3
[antwort_4] => 6
[antwort_5] => 8
)
[f_5] => Array
(
[antwort_1] => 9
[antwort_2] => 6
[antwort_3] => 9
[antwort_4] => 2
[antwort_5] => 9
)
)
ergebnis:
frage "f_5" wurden (nach gewicht) am besten beantwortet
*/
Alles anzeigen