Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在另一个多维数组的最后一行之前插入多维数组_Php_Arrays_Multidimensional Array_Array Splice - Fatal编程技术网

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

Php 在另一个多维数组的最后一行之前插入多维数组,php,arrays,multidimensional-array,array-splice,Php,Arrays,Multidimensional Array,Array Splice,假设我有两个多维数组: 主阵列($\u会话['Trials']) 临时阵列($currentTrial) 我想将临时数组插入主数组的倒数第二行/位置。请记住,两者都是多维的,并且具有以下格式: ( [Stimuli] => Array ( [Cue] => apple [Answer] => orange [Shuffle] => on

假设我有两个多维数组:

  • 主阵列($\u会话['Trials'])
  • 临时阵列($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] => 
        )
    
      [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、6和7)中的每一个都具有上面的格式,以及一个用于[刺激]、[过程]和[响应]的数组。我希望所有这些都在主阵列的位置5

    谢谢你的帮助

    编辑:

    简而言之,我有这个当前数组(我跳过了项目0-4,但它是相同的):

    )

    我希望它看起来像这样:

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

  • 5=刺激物
  • 6=程序
  • 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]);
    

    为了达到同样的效果,我建议您:

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

    为了达到同样的效果。

    如果您发布所需的数组格式和当前值,而不是显示摘录,这将非常清楚。当您在数组拼接调用中使用方括号括住currentTrial时,是否得到了正确的结果?我的意思是:
    array\u splice($\u SESSION['Trials',$minusOne,0,[$currentTrial])你好,我添加了澄清信息。谢谢你的帮助。我将尝试第一个答案并向您报告。jh1711-->感谢您发现缺少的括号!我相信这样做也会奏效。你很快就发现了,我从来没有想到过!谢谢你的帮助!如果您发布所需的数组格式和当前值,而不是显示摘录,这将非常清楚。当您在数组拼接调用中用方括号括住Count currentTrial时,是否得到了正确的结果?我的意思是:
    array\u splice($\u SESSION['Trials',$minusOne,0,[$currentTrial])你好,我添加了澄清信息。谢谢你的帮助。我将尝试第一个答案并向您报告。jh1711-->感谢您发现缺少的括号!我相信这样做也会奏效。你很快就发现了,我从来没有想到过!谢谢你的帮助!哇!我说不出话来。您的解决方案非常优雅,工作完美无瑕。非常感谢!编辑,所以我有正确的想法,但缺少括号!哈哈哇!我仍然喜欢你的解决方案,因为它需要更少的线,更整洁。哇!我说不出话来。您的解决方案非常优雅,工作完美无瑕。非常感谢!编辑,所以我有正确的想法,但缺少括号!哈哈哇!我仍然喜欢你的解决方案,因为它需要更少的线和更整洁。