2017-09-02 110 views
0

假设的最后一行之前多维数组我有两个多维数组:插入另一个多维数组

  1. 母版阵列($ _SESSION [ '试验'])
  2. 临时数组($ currentTrial)

我想将临时数组插入主数组的倒数第二行/位置。请记住,无论是多方面的,有这样的格式:

(
     [Stimuli] => Array 
      (
       [Cue] => apple 
       [Answer] => orange 
       [Shuffle] => on 
       [Stimuli Notes] => blank 
      ) 

     [Procedure] => Array 
      (
       [Item] => 3 
       [Trial Type] => Copy 
       [Timing] => User 
       [Post 1 Trial Type] => off 
       [Post 1 Timing] => User 
       [Text] => 
       [Procedure Notes] => 
       [Shuffle] => phase1 
       [Settings] => 
       [Post 1 Text] => 
      ) 

     [Response] => Array 
      (
       [Accuracy] => 
       [RT] => 
       [RTkey] => 
       [RTlast] => 
       [Response] => 
       [lenientAcc] => 
       [strictAcc] => 
      ) 

    ) 

到目前为止,我这样做:

$countArray = count($_SESSION['Trials']); 
$minusOne = $countArray-1; 
array_splice($_SESSION['Trials'], $minusOne, 0, $currentTrial); 

插入点是正确的,但它并没有保留临时数组的格式(而是,它脱离了临时数组成新的元素每隔更小的阵列)和看起来像这样:

[5] => Array 
    (
     [Cue] => hadithi 
     [Answer] => story 
     [Shuffle] => LithuanianEnglish 
     [Stimuli Notes] => 0.04-Hard 
    ) 

[6] => Array 
    (
     [Item] => 2 
     [Trial Type] => CritTest 
     [Max Time] => computer 
     [Min Time] => - 
     [Procedure Notes] => Criterion Test Trial 
     [Shuffle] => Session1Phase2 
     [Text] => 
    ) 

[7] => Array 
    (
     [RT] => 
     [Response] => 
     [Accuracy] => 
     [RTfirst] => 
     [RTlast] => 
     [strictAcc] => 
     [lenientAcc] => 
     [focus] => 
    ) 

我希望每个这些阵列(5,6,和7)具有格式化了上述无线为[Stimuli],[Procedure]和[Response]组成一个数组。我希望所有这些都在主阵列的第5位。

谢谢你的帮助!

编辑

总之,我有这样的电流阵列(我跳过项目0-4,但它是相同的):

[4] => Array 
    (
     [Stimuli] => Array 
      (
       [Cue] => gharika 
       [Answer] => flood 
       [Shuffle] => LithuanianEnglish 
       [Stimuli Notes] => 0.04-Hard 
      ) 

     [Procedure] => Array 
      (
       [Item] => 3 
       [Trial Type] => CritTest 
       [Max Time] => computer 
       [Min Time] => - 
       [Procedure Notes] => Criterion Test Trial 
       [Shuffle] => Session1Phase2 
       [Text] => 
      ) 

     [Response] => Array 
      (
       [RT] => 
       [Response] => 
       [Accuracy] => 
       [RTfirst] => 
       [RTlast] => 
       [strictAcc] => 
       [lenientAcc] => 
       [focus] => 
      ) 

    ) 

[5] => Array 
    (
     [Cue] => hadithi 
     [Answer] => story 
     [Shuffle] => LithuanianEnglish 
     [Stimuli Notes] => 0.04-Hard 
    ) 

[6] => Array 
    (
     [Item] => 2 
     [Trial Type] => CritTest 
     [Max Time] => computer 
     [Min Time] => - 
     [Procedure Notes] => Criterion Test Trial 
     [Shuffle] => Session1Phase2 
     [Text] => 
    ) 

[7] => Array 
    (
     [RT] => 
     [Response] => 
     [Accuracy] => 
     [RTfirst] => 
     [RTlast] => 
     [strictAcc] => 
     [lenientAcc] => 
     [focus] => 
    ) 

[8] => Array 
    (
     [Stimuli] => Array 
      (
       [Cue] => gharika 
       [Answer] => flood 
       [Shuffle] => LithuanianEnglish 
       [Stimuli Notes] => 0.04-Hard 
      ) 

     [Procedure] => Array 
      (
       [Item] => ExperimentFinished 
       [Trial Type] => CritTest 
       [Max Time] => computer 
       [Min Time] => - 
       [Procedure Notes] => Criterion Test Trial 
       [Shuffle] => Session1Phase2 
       [Text] => 
      ) 

     [Response] => Array 
      (
       [RT] => 
       [Response] => 
       [Accuracy] => 
       [RTfirst] => 
       [RTlast] => 
       [strictAcc] => 
       [lenientAcc] => 
       [focus] => 
      ) 

    ) 

我希望它看起来像这样:

​​

请注意元素#5为刺激,过程和响应保存的数组。目前,它打破了#5成这样:

  1. 5 =刺激
  2. 6 =程序
  3. 7 =响应

我想在#5所有这一切,和#6仍然是主数组中的最后一个元素。我想将$ currentTrial添加到主数组中并保持相同的多维格式。

+0

如果您发布所需的数组格式和当前,而不是显示摘录,这将是__much__明确。 –

+2

当你在你的array_splice调用中放置方括号时,你会得到正确的结果吗?我的意思是:'array_splice($ _ SESSION ['Trials'],$ minusOne,0,[$ currentTrial]);' – jh1711

+0

您好,我添加了澄清信息。谢谢您的帮助。我会尝试第一个答案并回报。 –

回答

4

我建议你这样做:

// Here you get last item from session array 
// Session array becomes smaller by one element 
$last_item = array_pop($_SESSION['Trials']); 
// add `$currentTrial` to end of array, and then add `$last_item` 
array_push($_SESSION['Trials'], $currentTrial, $last_item); 

正如已经被@ jh1711你原来的代码中的注释中提到可以改为:

array_splice($_SESSION['Trials'], $minusOne, 0, [$currentTrial]); 

来达到同样的效果。

+0

哇!我无语。您的解决方案非常高雅,工作完美无瑕。非常感谢! *编辑*所以我有正确的想法,但失去了括号!哈哈哇!我仍然喜欢你的解决方案,因为它需要更少的线路并且更加整洁。 –