Предположим, у меня есть два многомерных массива:
Я хочу вставить временный массив в предпоследнюю строку / позицию мастер-массива. Имейте в виду, что оба они многомерны и имеют такой формат:
(
[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], [Процедура] и [Ответ]. Я хочу, чтобы все это было в позиции 5 Master Array.
Спасибо за любую помощь!
редактировать:
Короче говоря, у меня есть этот текущий массив (я пропустил пункты 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:
Я хочу, чтобы все это в # 5 и # 6 оставалось последним элементом в массиве master. Я хочу добавить $ 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]);
добиться того же эффекта.
Других решений пока нет …