php - 我如何重组这个数组以将数据压缩成 PHP 中更有用的格式?

标签 php arrays multidimensional-array

我会先尝试解释我正在处理的数据,然后我会解释我希望用这些数据做什么,然后我会解释我到目前为止所做的尝试。希望有人能指出我正确的方向。


我在做什么:

我有一个包含调查回复的数组。前两项是第一个问题的两个答案,responses 包含选择这些答案的人数。最后三项是我们提出的另一个问题的三个答案。

Array
(
[0] => Array
    (
        [survey_id] => 123456789
        [question_text] => Have you made any changes in how you use our product this year?
        [d_answer_text] => No
        [responses] => 92
    )

[1] => Array
    (
        [survey_id] => 123456789
        [question_text] => Have you made any changes in how you use our product this year?
        [answer_text] => Yes
        [responses] => 30
    )

[2] => Array
    (
        [survey_id] => 123456789
        [question_text] => How would you describe your interaction with our staff compared to prior years?
        [answer_text] => Less Positive
        [responses] => 14
    )

[3] => Array
    (
        [survey_id] => 123456789
        [question_text] => How would you describe your interaction with our staff compared to prior years?
        [answer_text] => More Positive
        [responses] => 35
    )

[4] => Array
    (
        [survey_id] => 123456789
        [question_text] => How would you describe your interaction with our staff compared to prior years?
        [answer_text] => No Change
        [responses] => 72
    )

)


我想要实现的目标:

我想创建一个数组,其中 question_text 用作键(或者我可能获取 question_id 并改为使用它),使用 answer_text 作为键,responses 作为值。它看起来像这样:

Array
(
[Have you made any changes in how you use our product this year?] => Array
    (
        [No] => 92
        [Yes] => 30
    )

[How would you describe your interaction with our staff compared to prior years?] => Array
    (
        [Less Positive] => 14
        [More Positive] => 35
        [No Change] => 72
    )

)


这是我尝试过的:

$response_array = array();
foreach($result_array as $value){
    //$responses_array['Our question'] = array('answer 1'=>responses,'answer 2'=>responses);
    $responses_array[$value['question_text']] = array($value['answer_text']=>$value['responses']);
}

这不起作用,因为每个循环都会覆盖 $responses_array[$question] 的值。这对我来说很有意义,我明白为什么它不起作用。

我的下一个想法是尝试使用 array_merge()

$responses_array = array();
foreach($result as $value){
    $question_text = $value['question_text'];
    $answer_text = $value['answer_text'];
    $responses = $value['responses'];
    $responses_array[$question_text] = array_merge(array($responses_array[$question_text],$answer_text=>$responses));
}

我想我的逻辑是错误的,因为看起来数组嵌套太多了。

Array
(
    [Have you made any changes in how you use our product this year?] => Array
        (
            [0] => Array
                (
                    [0] => 
                    [No] => 92
                )

            [Yes] => 30
        )

我对 array_merge 的问题是我无法在 foreach 循环的每次迭代中访问问题的所有答案。

如果我们引入更多具有不同数量答案的问题,我想以一种允许扩展的方式设计它。如何解决?

最佳答案

听起来像是一份减少的工作

$response_array = array_reduce($result_array, function($carry, $item) {
  $carry[$item['question_text']][$item['answer_text']] = $item['responses'];
  return $carry;
}, []);

演示~ https://eval.in/687264

关于php - 我如何重组这个数组以将数据压缩成 PHP 中更有用的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855133/

相关文章:

php - 从 mysql 查询创建 token 列表到 json

php - Laravel 获取每一列的最高值

c - 改变C中数组的大小

TypeScript:多维数组参数的正确方法

php 将日期转换为时间戳

php - 为什么透明背景不适用于 GD?

php - Laravel - 更新输入文件中上传的文件

java可比较错误,在完成之前停止排序

php - 检查两个表中两列的值是否相等然后返回

php - 如何删除空的关联数组条目