【发布时间】:2017-06-17 19:21:55
【问题描述】:
我目前有一个存储大量问题、问题类型和答案的数组。现在答案可以有不同的长度。例如:
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"
我面临的问题是每个问题都包含一个question、type 和correct 值,但答案的数量可能不同 - 可能有 10 多个答案。
我目前的方法是遍历数组并为问题、类型和更正创建新数组,然后循环遍历数组并通过键配对问题的值。由于存在不同数量的答案,不幸的是,这并没有带来好处。
有什么可能的方法来解决这个问题?如何将所有值与适当的问题相关联?
非常感谢您的帮助和提示!
【问题讨论】:
-
为什么键 36 在键 12 和键 13 之间?
-
发布预期结果
-
@mickmackusa 有 222 个项目,这不是问题。但确保第一个正则表达式可以替换为 strpos 甚至 substr。无论哪种方式,我都怀疑它会对现实生活产生很大影响。
-
还有米克,你看到你的代码的性能时间了吗? 3v4l.org/0eJvo/perf#output 。可怜又没有灵感啊?....
-
@Andreas Phew,有那么一刻我以为世界颠倒了......我通过将 3 个问题的批次乘以 8 倍来任意扩展输入数组以进行性能测试。 Andreas' code My code 在我看来,我的答案表现得更好,但我没有对每个代码进行 50 次计时然后取平均值。我之前的断言归结为迭代函数调用的数量,我觉得运行这么多单独的 preg_match 调用而不是一个大的调用并不是最佳实践。