2017-09-02 180 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

哇!我無語。您的解決方案非常高雅,工作完美無瑕。非常感謝! *編輯*所以我有正確的想法,但失去了括號!哈哈哇!我仍然喜歡你的解決方案,因爲它需要更少的線路並且更加整潔。 –