首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP:将数组求和为多维数组

PHP:将数组求和为多维数组
EN

Stack Overflow用户
提问于 2018-07-06 04:02:55
回答 1查看 35关注 0票数 -1

我有一个存储章节、问题和答案的数组。正如您在示例中所看到的,这并不是很方便。

我想要的是一个多维数组。

在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'
                )
            )
        )
    )
);    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-06 04:33:31

好吧,我会使用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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51199046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档