【问题标题】:How to associate array keys to a specific prior key?如何将数组键关联到特定的先前键?
【发布时间】: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"

我面临的问题是每个问题都包含一个questiontypecorrect 值,但答案的数量可能不同 - 可能有 10 多个答案。

我目前的方法是遍历数组并为问题、类型和更正创建新数组,然后循环遍历数组并通过键配对问题的值。由于存在不同数量的答案,不幸的是,这并没有带来好处。

有什么可能的方法来解决这个问题?如何将所有值与适当的问题相关联?

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

【问题讨论】:

  • 为什么键 36 在键 12 和键 13 之间?
  • 发布预期结果
  • @mickmackusa 有 222 个项目,这不是问题。但确保第一个正则表达式可以替换为 strpos 甚至 substr。无论哪种方式,我都怀疑它会对现实生活产生很大影响。
  • 还有米克,你看到你的代码的性能时间了吗? 3v4l.org/0eJvo/perf#output 。可怜又没有灵感啊?....
  • @Andreas Phew,有那么一刻我以为世界颠倒了......我通过将 3 个问题的批次乘以 8 倍来任意扩展输入数组以进行性能测试。 Andreas' code My code 在我看来,我的答案表现得更好,但我没有对每个代码进行 50 次计时然后取平均值。我之前的断言归结为迭代函数调用的数量,我觉得运行这么多单独的 preg_match 调用而不是一个大的调用并不是最佳实践。

标签: php arrays regex


【解决方案1】:

也许你可以把这样的问题分组?
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 秒。
你是法官克里斯,有些人发誓正则表达式会在不知道或不尝试的情况下放慢速度。

【讨论】:

  • @Chris 谢谢!我不知道您打算如何使用该数组,但也许这是更好的方法? 3v4l.org/dvZVZ
  • 使用strpos($value,'(Type')===0不是更快吗?
  • @mickmackusa 也许是。但它只有 222 项要检查。我认为任何其他没有 100% 优化的代码都很容易吃掉速度上的任何差异。
  • 在没有来自 OP 的更多信息的情况下,我现在比我更喜欢你的方法。 (+1) 如果项目是我的,我会这样修改你的方法:sandbox.onlinephpfunctions.com/code/…
猜你喜欢
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多