douweida2669 2016-11-28 23:10
浏览 115
已采纳

如何重构此数组以在PHP中将数据压缩为更有用的格式?

I will try to explain the data I'm working with first, then I'll explain what I hope to do with the data, then I'll explain what I've tried so far. Hopefully someone can point me in the right direction.


What I'm working with:

I have an array containing survey responses. The first two items are the two answers for the first question and responses contains the number of people who selected those answers. The last three items are the three answers for the other question we asked.

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
    )

)


What I want to achieve:

I want to create an array where the question_text is used as the key (or I might grab the question_id and use it instead), use the answer_text as a key, with the responses as the value. It would look something like this:

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
    )

)


Here's what I've tried:

$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']);
}

This does not work because each loop will overwrite the value for $responses_array[$question]. This makes sense to me and I understand why it won't work.

My next thought was to try using 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));
}

I guess my logic was wrong because it looks like the array is nesting too much.

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

            [Yes] => 30
        )

My problem with array_merge is that I don't have access to all answers for the question in each iteration of the foreach loop.

I want to design this in a way that allows it to scale up if we introduce more questions with different numbers of answers. How can this be solved?

  • 写回答

3条回答 默认 最新

  • douluanzhao6689 2016-11-28 23:42
    关注

    Sounds like a reduce job

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

    Demo ~ https://eval.in/687264

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作