2017-09-02 5 views
0

Angenommen, ich habe zwei mehrdimensionale Arrays:Einfügen von mehrdimensionalen Arrays vor der letzten Zeile eines anderen mehrdimensionalen Arrays

  1. Master-Array ($ _SESSION [ 'Trials'])
  2. temporäre Array ($ currentTrial)

Ich möchte das temporäre Array in die vorletzte Zeile/Position des Master-Arrays einfügen. Denken Sie daran, beide sind mehrdimensional und haben das folgende Format:

(
     [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] => 
      ) 

    ) 

Bisher habe ich dies:

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

Die Einfügemarke ist richtig, aber es hat nicht erhalten das Format des temporären Arrays (und stattdessen brach er jede kleinere Array aus temporären Array in neue Elemente) und sieht wie folgt aus:

[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] => 
    ) 

ich möchte jedes dieser Arrays (5, 6 und 7) die Formatierung über wi bis haben th ein Array für [Stimuli], [Procedure] und [Response]. Ich möchte, dass all das in Position 5 des Master-Arrays ist.

Vielen Dank für jede Hilfe!

bearbeiten:

Kurz gesagt, ich diese aktuelle Array (ich übersprungen Artikel 0-4, aber es ist das gleiche):

[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] => 
      ) 

    ) 

)

ich möchte es aussehen wie folgt:

[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 
    (
    [Stimuli] => Array 
       [Cue] => hadithi 
       [Answer] => story 
       [Shuffle] => LithuanianEnglish 
       [Stimuli Notes] => 0.04-Hard 
    ) 

    [Procedure] => Array 
       [Item] => 2 
       [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] => 
    ) 

[6] => 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] => 
      ) 

    ) 

) 

Beachten Sie, dass Element # 5 hat ein erhaltenes Array für Stimuli, Prozedur und Antwort. Derzeit sind es # 5 in diese Zerschlagung:

  1. 5 = Stimuli
  2. 6 = Verfahren
  3. 7 = Antwort

ich alle wollen, dass in # 5 und # 6 Bleibt das letzte Element im Master-Array. Ich möchte $ currentTrial in das Master-Array einfügen und dasselbe mehrdimensionale Format beibehalten.

+0

Es wäre __much__ klar, wenn Sie das erforderliche Array-Format und den aktuellen Wert angeben, anstatt Auszüge anzuzeigen. –

+2

Erhalten Sie das korrekte Ergebnis, wenn Sie currentTrial in Ihrem Array_splice-Aufruf in eckige Klammern setzen?Ich meine: 'array_splice ($ _ SESSION ['Trials'], $ minusOne, 0, [$ currentTrial]);' – jh1711

+0

Hallo, ich habe klärende Informationen hinzugefügt. Vielen Dank für Ihre Hilfe. Ich werde die erste Antwort versuchen und berichten. –

Antwort

4

rate ich Ihnen, dies zu tun:

// 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); 

Wie bereits in den Kommentaren erwähnt von @ jh1711 ursprünglichen Code kann geändert werden:

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

die gleiche Wirkung zu erzielen.

+0

Wow! Ich bin sprachlos. Ihre Lösung ist äußerst elegant und funktioniert einwandfrei. Danke vielmals! * Edit * Also hatte ich die richtige Idee aber es fehlten Klammern! Haha wow! Ich bevorzuge immer noch Ihre Lösung, da sie weniger Linien erfordert und aufgeräumter ist. –

Verwandte Themen