如何将数组键与特定的先前键关联?

时间:2017-06-17 19:21:56

标签: php arrays regex

我目前有一个存储很多问题,问题类型和答案的数组。现在答案可能有不同的长度。例如:

  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"

我遇到的问题是,每个问题都包含questiontypecorrect值,但可能会有不同数量的答案 - 可能有10个以上的答案。< / p>

我目前的做法是通过数组并为问题,类型和更正创建新数组,然后通过键循环遍历数组和配对问题的值。由于存在不同数量的答案,遗憾的是这并不是有益的。

这可能是什么方法?如何将所有值与相应的问题相关联?

非常感谢任何帮助和提示!

1 个答案:

答案 0 :(得分:2)

也许你可以将这样的问题分组?
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);

编辑删除一个正则表达式。 但是根据3v4l,系统时间从0.003秒变为0.040秒 你是裁判克里斯,有些人发誓正规则在不知情或不尝试的情况下放慢速度。