在另一个多维数组的最后一行之前插入多维数组

Inserting multidimensional array before last line of another multidimensional array

假设我有两个多维数组:

  1. 主数组($_SESSION['Trials'])
  2. 临时数组($currentTrial)

我想将临时数组插入主数组的倒数第二个line/position。请记住,两者都是多维的并且具有以下格式:

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

    )

)

我希望它看起来像这样:

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

    )

)

请注意,元素 #5 有一个保留的刺激、过程和响应数组。目前,它将 #5 分解为:

  1. 5 = 刺激
  2. 6 = 过程
  3. 7 = 回应

我希望#5 和#6 中的所有元素都保留在主数组中的最后一个元素。我想将 $currentTrial 添加到主数组中并保持相同的多维格式。

我建议你这样做:

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

达到同样的效果。