How to associate array keys to a specific prior key?

I currently have an array that stores a lot of questions, question types, and answers. Now the answers can be of different lengths. For example:

  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"

The problem I face is that each question includes a question, type and correct value, but the there could be varying amounts of answers - 10+ answers could be possible.

My current approach was to work through the array and create new arrays for for questions, types, and corrects, and then loop through the arrays and pair pair question's values by the keys. Due to the fact there being different amounts of answers, this unfortunately did not end of being beneficial.

What could be a possible approach to this? How can I associate all the values to the appropriate question?

Thanks so much for any help and hints!

1 answer

  • answered 2017-06-17 19:21 Andreas

    Maybe you can group the questions like this?
    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 to remove one regex. However according to 3v4l the system time has gone from 0.003 seconds to 0.040 seconds with this change.
    You be the judge Chris, some people swear on regex slowing down without knowing or trying.