2017-06-17 4 views
0

Ich habe derzeit ein Array, das viele Fragen, Fragetypen und Antworten speichert. Jetzt können die Antworten unterschiedlich lang sein. Zum Beispiel:Wie ordnen Sie Array-Schlüssel einem bestimmten vorherigen Schlüssel zu?

0 => array:222 [▼ 
    0 => "(Type): multiplechoice" 
    1 => "(Question): Which of the following is true about pre-test imagery?" 
    2 => "(A): People with a cranial fault can visualize the muscle being strong and make it go strong, while people without a cranial faults cannot." 
    3 => "(B): If Governing Vessel (GV) 20 tests weak, it also means they have a cranial fault. This is on the midline at the apex of the head." 
    4 => "(C): The three most common cranial faults are TMJ, occiput and sphenoid" 
    5 => "(D): All of the above" 
    6 => "(Correct): D" 
    7 => "(Type): multiplechoice" 
    8 => "(Question): Which of the following is not true about the TMJ?" 
    9 => "(A): Every single TMJ nerve pathway goes through the mesencephalon in the midbrain." 
    10 => "(B): When you work on the TMJ it is a neurological back-up for the entire body." 
    11 => "(C): To correct press on the glabella with one hand as you press of the back of the head with the other as the patient touches the chin with two fingers and breathes in only one time." 
    12 => "(Correct): C" 
    13 => "(Type): truefalse" 
    14 => "(Question): To test for a deficiency of the four primary neurotransmitters, point the edge of a magnet straight in at each of the four corresponding cranial bon ▶" 
    15 => "(A): True" 
    16 => "(B): False" 
    17 => "(Correct): A" 

Das Problem, das ich Gesicht ist, dass jede Frage einen question, type und correct Wert enthält, aber die könnten es unterschiedliche Mengen an Antworten sein - 10+ Antworten möglich sein könnten.

Mein aktueller Ansatz war es, durch das Array zu arbeiten und neue Arrays für Fragen, Typen und Korrekte zu erstellen, und dann durch die Arrays und Pair Pair Fragen Frage Werte durch die Schlüssel. Aufgrund der Tatsache, dass es unterschiedliche Antworten gibt, endete dies leider nicht von Vorteil.

Was könnte ein möglicher Ansatz dazu sein? Wie kann ich alle Werte der entsprechenden Frage zuordnen?

Vielen Dank für jede Hilfe und Hinweise!

+0

Warum ist die Taste 36 zwischen Taste 12 und 13? – Andreas

+0

post das erwartete Ergebnis – RomanPerekhrest

+0

@mickmackusa mit 222 Elementen, die kein Problem ist. Aber sicher kann der erste Regex durch einen strpos oder sogar substr ersetzt werden. So oder so, ich bezweifle, dass es im wirklichen Leben viel Unterschied machen wird. – Andreas

Antwort

2

Vielleicht können Sie die Fragen so gruppieren?
https://3v4l.org/dvZVZ

$arr = array(
0 => "(Type): multiplechoice", 
1 => "(Question): Which of the following is true about pre-test imagery?", 
2 => "(A): People with a cranial fault can visualize the muscle being strong and make it go strong, while people without a cranial faults cannot.", 
3 => "(B): If Governing Vessel (GV) 20 tests weak, it also means they have a cranial fault. This is on the midline at the apex of the head.", 
4 => "(C): The three most common cranial faults are TMJ, occiput and sphenoid", 
5 => "(D): All of the above", 
6 => "(Correct): D", 
7 => "(Type): multiplechoice", 
8 => "(Question): Which of the following is not true about the TMJ?", 
9 => "(A): Every single TMJ nerve pathway goes through the mesencephalon in the midbrain.", 
10 => "(B): When you work on the TMJ it is a neurological back-up for the entire body.", 
11 => "(C): To correct press on the glabella with one hand as you press of the back of the head with the other as the patient touches the chin with two fingers and breathes in only one time.", 
12 => "(Correct): C", 
13 => "(Type): truefalse", 
14 => "(Question): To test for a deficiency of the four primary neurotransmitters, point the edge of a magnet straight in at each of the four corresponding cranial bon ▶", 
15 => "(A): True", 
16 => "(B): False", 
17 => "(Correct): A"); 


$j=-1; 
$res = array(); 
Foreach($arr as $value){ 
    If(substr($value,0,6)== "(Type)"){ 
     $j++; 
    } 
    preg_match("/\((.*?)\): (.*)/", $value, $match); 
    $res[$j][$match[1]]= $match[2]; 

} 

Var_dump($res); 

Edited eine Regex zu entfernen. Allerdings ist nach 3v4l die Systemzeit von 0,003 Sekunden auf 0,040 Sekunden mit dieser Änderung gegangen.
Sie sind der Richter Chris, einige Leute schwören auf Regex Verlangsamung ohne zu wissen oder zu versuchen.

+0

@Chris danke! Ich weiß nicht, wie Sie das Array verwenden wollten, aber vielleicht ist das ein besserer Weg? https://3v4l.org/dvZVZ – Andreas

+0

Wäre es nicht schneller zu verwenden 'strpos ($ value, '(Type') === 0'? – mickmackusa

+0

@mickmackusa vielleicht ist es. Aber es ist nur 222 Artikel zu überprüfen. Ich denke, jeder Unterschied in der Geschwindigkeit kann leicht aufgefressen werden, wenn ein anderer Code nicht zu 100% optimiert ist. " – Andreas

Verwandte Themen