Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Associative Array - Fatal编程技术网

如何将数组转换为多维PHP

如何将数组转换为多维PHP,php,arrays,multidimensional-array,associative-array,Php,Arrays,Multidimensional Array,Associative Array,这是我当前的数组,我想把这个数组转换成多维数组 Array ( [question1] => My question 1 [options1] => My Option 1 [answer1] => Answer 1 goes here [question2] => My question 2 [options2] => My Option 2 [answer2] => Answer 2 goes here )

这是我当前的数组,我想把这个数组转换成多维数组

Array
(
    [question1] => My question 1
    [options1] => My Option 1
    [answer1] => Answer 1 goes here
    [question2] =>  My question 2
    [options2] => My Option 2
    [answer2] => Answer 2 goes here
)
我希望我的数组如下所示。如何做到这一点,有什么建议吗

Array
(
    [0] => Array
        (
          [question1] => My question 1
          [options1] => My Option 1
          [answer1] => Answer 1 goes here
        )

    [1] => Array
       (
           [question2] =>  My question 2
           [options2] => My Option 2
           [answer2] => Answer 2 goes here
       )
 )
这是我的密码

$i=9;
$topicsArr=array();
$j = 1;
while ($row[$i]){
    $topicsArr['question' .$j] = $row[$i];
    $topicsArr['options' .$j] = $row[$i+1];
    $topicsArr['answer' .$j] = $row[$i+2];
    $i = $i +3;
    $j++;
}
您可以使用array_chunk()


只需使用aux变量即可保存所有子元素:

$i=9;
$topicsArr=array();
$j = 1;
while ($row[$i]){
    $aux = array();
    $aux['question'] = $row[$i];
    $aux['options'] = $row[$i+1];
    $aux['answer'] = $row[$i+2];
    $topicsArr.push($aux);
    $i = $i +3;
    $j++;
}

我不认为你们真的想要输出表中的“问题1”。我想你会想要“问题”来代替。但是,如果您想要“问题1”等,请将行“$tmp[$key]”更改为“$tmp[$key.$i]”。“for”循环也应该“在现实生活中”更改,因为我想您将拥有更多的键,但下面的代码为您提供了一个良好的开端:)

$input=[
“问题1”=>“我的问题1”,
'选项1'=>'我的选项1',
“答案1”=>“答案1在这里”,
“问题2”=>“我的问题2”,
'选项2'=>'我的选项2',
“答案2”=>“答案2在这里”
];
$output=[];
对于($i=1;$i)
$i=9;
$topicsArr=array();
$j = 1;
while ($row[$i]){
    $aux = array();
    $aux['question'] = $row[$i];
    $aux['options'] = $row[$i+1];
    $aux['answer'] = $row[$i+2];
    $topicsArr.push($aux);
    $i = $i +3;
    $j++;
}
$input = [
    'question1' => 'My question 1',
    'options1' => 'My Option 1',
    'answer1' => 'Answer 1 goes here',
    'question2' => 'My question 2',
    'options2' => 'My Option 2',
    'answer2' => 'Answer 2 goes here'
];
$output = [];
for ($i=1; $i<=2; $i++) {
    $tmp = [];
    foreach (['question', 'options', 'answer'] as $key) {
        $tmp[$key] = $input[$key.$i];
    }
    $output[] = $tmp;
}