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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀