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

时间:2016-11-28 23:10:08

标签: 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循环的每次迭代中,我都无法访问问题的所有答案。

我想以一种允许它扩展的方式来设计它,如果我们引入更多不同答案数的问题。怎么解决这个问题?

3 个答案:

答案 0 :(得分:3)

听起来像 reduce 作业

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

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

答案 1 :(得分:2)

<强>更新

删除条件(参见@Phil评论)

我认为你正在寻找类似的东西:

$output = [];

for($i = 0; $i < count($array); $i++) {
    $output[$array[$i]['question_text']] [$array[$i]['answer_text']]= $array[$i]['responses'];
}

print_r($output);

答案 2 :(得分:0)

与发布的答案略有不同,更符合您已经尝试过的内容。试试这个:

$responses_array = array();
$sub_array = array();
$index = $result[0]['question_text'];    

foreach($result as $value){

    $question_text = $value['question_text'];
    $answer_text = $value['answer_text'];
    $responses = $value['responses'];
    if (strcmp($index, $question_text) == 0) {
        $sub_array[$answer_text] = $responses;
    } else {
        $index = $question_text;
        $responses_array[$index] = $sub_array;
        $sub_array = array();
    }
}

编辑:发现我的错误,稍微更新了我的答案,希望这会有用。

编辑2:在此处使用示例:https://eval.in/687275