2017-06-17 79 views
0

我目前有一个存储大量问题,问题类型和答案的数组。现在答案可以有不同的长度。例如:如何将数组键与特定的先前键相关联?

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+答案是可能的。

我目前的做法是通过数组工作并为问题,类型和更正创建新的数组,然后循环访问数组并通过键对pair问题的值。由于事实上存在不同数量的答案,所以这不幸没有结束。

这可能是一种可能的方法?我怎样才能将所有的价值观与适当的问题联系起来?

非常感谢您的帮助和提示!

+0

为什么钥匙36位于钥匙12和13之间? – Andreas

+0

发布预期结果 – RomanPerekhrest

+0

@mickmackusa拥有222项内容并不是问题。但确定第一个正则表达式可以用strpos或者substr来替换。无论哪种方式,我怀疑它会在现实生活中有很大的变化。 – Andreas

回答

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秒。
你是裁判克里斯,有些人在正则表​​达式中发誓放弃而不知道或尝试。

+0

@Chris谢谢你!我不知道你打算如何使用阵列,但也许这是一个更好的方法? https://3v4l.org/dvZVZ – Andreas

+0

使用'strpos($ value,'(Type')=== 0''不会更快吗? – mickmackusa

+0

@mickmackusa也许是这样,但它只有222个项目需要检查。我认为任何速度上的差异很容易被任何其他代码没有被100%优化所吞噬。 – Andreas

相关问题