Inserting multidimensional array before last line of another multidimensional array

48 views Asked by At

Assume I have two multidimensional arrays:

  1. Master Array ($_SESSION['Trials'])
  2. Temporary Array ($currentTrial)

I want to insert the temporary array into the penultimate line/position of the master array. Bear in mind, both are multidimensional and have this 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] => 
            )

    )

So far, I did this:

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

The insertion point is correct, but it did not preserve the format of the temporary array (and instead, it broke every smaller array from temporary array into new elements) and looks like this:

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

I want each of those arrays (5, 6, and 7) to have the formatting up above with an array for [Stimuli], [Procedure], and [Response]. I want all of that to be in position 5 of the Master Array.

Thank you for any help!

Edit:

In short, I have this current array (I skipped items 0-4 but it's the same):

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

    )

)

I want it to look like this:

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

    )

)

Notice that element #5 has a preserved Array for Stimuli, Procedure, and Response. Currently, it's breaking up #5 into this:

  1. 5 = Stimuli
  2. 6 = Procedure
  3. 7 = Response

I want all of that in #5, and #6 to remain the last element in the master array. I want to add $currentTrial into the master array and keep the same multidimensional format.

1

There are 1 answers

1
u_mulder On BEST ANSWER

I advise you to do this:

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

As already mentioned in comments by @jh1711 your original code can be changed to:

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

to achieve the same effect.