Php 如何将索引数组转换为关联数组
我有这样一个数组:Php 如何将索引数组转换为关联数组,php,arrays,Php,Arrays,我有这样一个数组: array:10 [ 1 => "1. What is the first question?" 2 => "A. First answer" 3 => "B. Second answer" 4 => "C. Third answer" 5 => "D. Forth answer" 6 => "ANSWER: A" 7 => "2. Wh
array:10 [
1 => "1. What is the first question?"
2 => "A. First answer"
3 => "B. Second answer"
4 => "C. Third answer"
5 => "D. Forth answer"
6 => "ANSWER: A"
7 => "2. What is the second question? This question is of multiple lines and it can
go into 2 or 3 or 4 lines, but make sure you import it correctly."
8 => "A. Yes, I got it"
9 => "B. No, I can’t"
10 => "ANSWER: A"
]
array:2[
'questions' => [
0 => [
'question_text' => '1. What is the first question?',
'options' => [
0 => 'A. First answer',
1 => 'B. Second answer',
2 => 'C. Third answer',
3 => 'D. Forth answer'
],
'answer' => 'ANSWER: A'
],
1 => [
'question_text' => '2. What is the second question? This question is of
multiple lines and it can go into 2 or 3 or 4 lines, but
make sure you import it correctly."',
'options' => [
0 => 'A. Yes, I got it',
1 => 'B. No, I can’t',
],
'answer' => 'ANSWER: A'
],
]
]
我想实现这样的阵列:
array:10 [
1 => "1. What is the first question?"
2 => "A. First answer"
3 => "B. Second answer"
4 => "C. Third answer"
5 => "D. Forth answer"
6 => "ANSWER: A"
7 => "2. What is the second question? This question is of multiple lines and it can
go into 2 or 3 or 4 lines, but make sure you import it correctly."
8 => "A. Yes, I got it"
9 => "B. No, I can’t"
10 => "ANSWER: A"
]
array:2[
'questions' => [
0 => [
'question_text' => '1. What is the first question?',
'options' => [
0 => 'A. First answer',
1 => 'B. Second answer',
2 => 'C. Third answer',
3 => 'D. Forth answer'
],
'answer' => 'ANSWER: A'
],
1 => [
'question_text' => '2. What is the second question? This question is of
multiple lines and it can go into 2 or 3 or 4 lines, but
make sure you import it correctly."',
'options' => [
0 => 'A. Yes, I got it',
1 => 'B. No, I can’t',
],
'answer' => 'ANSWER: A'
],
]
]
我想将此索引数组转换为多维数组。如何转换此数组?它不会是这么大的意大利面代码,即使它不适合两行: 结果:
array(1) {
["questions"]=>
array(2) {
[0]=>
array(3) {
["question_text"]=>
string(30) "1. What is the first question?"
["options"]=>
array(4) {
[0]=>
string(15) "A. First answer"
[1]=>
string(16) "B. Second answer"
[2]=>
string(15) "C. Third answer"
[3]=>
string(15) "D. Forth answer"
}
["answer"]=>
string(9) "ANSWER: A"
}
[1]=>
array(3) {
["question_text"]=>
string(158) "2. What is the second question? This question is of multiple lines and it can
go into 2 or 3 or 4 lines, but make sure you import it correctly."
["options"]=>
array(2) {
[0]=>
string(16) "A. Yes, I got it"
[1]=>
string(16) "B. No, I can’t"
}
["answer"]=>
string(9) "ANSWER: A"
}
}
}
尽管如此,这当然是一项非常特殊的任务,我同意前一位发言者的观点,即阵列是不寻常地指定的。但是如果是这样的话,你必须遵守这些准则……我不知道如何得到这个数组,但要转换它,你最终将编写只适用于一组特定数据的意大利面代码,你必须添加越来越多的逻辑来处理所有情况。我建议重新使用生成数组的方式,而不是在以后转换数组。基本上,我的主要功能是在数据库中插入.Docx文件。我有这种类型的.DOcx文件。所以我将Docx文件转换为数组@N@scode2704我们可以为您的输入添加一个特定的解决方案,但前提是您的数组格式在任何情况下都发生了更改,那么该解决方案将无法工作。请发布您尝试的代码。