PHP:将数组汇总为多维数组

时间:2018-07-05 20:02:55

标签: php arrays multidimensional-array

我有一个存储章节,问题和答案的数组。如您在示例中所见,这不是很方便。
我想拥有一个多维数组。

在PHP中执行此操作的最佳做​​法是什么?

我所拥有的:

array(
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 1',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 1',
        'answer' => 'Answer 2'
    ),
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 2',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 2',
        'question' => 'Question 1',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 2',
        'question' => 'Question 1',
        'answer' => 'Answer 2'
    )
);

我想要拥有的东西:

array(
    array(
        'chapter' => 'Chapter 1',
        'questions' => array(
            array(
                'text' => 'Question 1',
                'answers' => array(
                    'Answer 1',
                    'Answer 2'
                )
            ),
            array(
                'text' => 'Question 2',
                'answers' => array(
                    'Answer 1'
                )
            )
        )
    ),
    array(
        'chapter' => 'Chapter 2',
        'questions' => array(
            array(
                'text' => 'Question 1',
                'answers' => array(
                    'Answer 1',
                    'Answer 2'
                )
            )
        )
    )
);    

1 个答案:

答案 0 :(得分:1)

好吧,我会混合使用array_reduce,array_map和array_values:

<?php

$input = array(
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 1',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 1',
        'answer' => 'Answer 2'
    ),
    array(
        'chapter' => 'Chapter 1',
        'question' => 'Question 2',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 2',
        'question' => 'Question 1',
        'answer' => 'Answer 1'
    ),
    array(
        'chapter' => 'Chapter 2',
        'question' => 'Question 1',
        'answer' => 'Answer 2'
    )
);

$result = array_reduce(
  $input,
  function($output, $inputElem) {
    $output[$inputElem['chapter']]['chapter'] = $inputElem['chapter'];
    $output[$inputElem['chapter']]['questions'][$inputElem['question']]['text'] = $inputElem['question'];
    $output[$inputElem['chapter']]['questions'][$inputElem['question']]['answers'][] = $inputElem['answer'];
    return $output;
  },
  []
);

/* So now $result looks like this:
array(
  "Chapter 1"=> array(
    "chapter"=> "Chapter 1"
    "questions"=> array(
      "Question 1"=> array(
        "text"=> "Question 1"
        "answers"=> array(
          "Answer 1",
          "Answer 2"
        )
      )
      "Question 2"=> array(
        "text"=> "Question 2"
        "answers"=> array(
          "Answer 1"
        )
      )
    )
  )
  "Chapter 2"=> array(
    "chapter"=> "Chapter 2"
    "questions"=> array(
      "Question 1"=> array(
        "text"=> "Question 1"
        "answers"=> array(
          "Answer 1",
          "Answer 2"
        )
      )
    )
  )
)
*/

//we need to remove redundant keys now
$result = array_values( //removes main "chapter" keys
  array_map(
    function($chapter) {
      $chapter['questions'] = array_values($chapter['questions']); //removes "question" keys
      return $chapter;
    },
    $result
  )
);
var_dump($result);