2016-03-08 100 views
1

结合需要一些帮助,不断来回的办法可能做到这一点。我正在FileMaker中创建一个基本的调查屏幕,并使用PHP来获得我的结果。我需要以可用格式整理调查数据,以便绘制数据图表。当我使用PHP查询FileMaker时,它会返回一个相当大的数据。下面我的代码已经成功地输出如下:PHP循环通过对结果

foreach ($data as $key => $question) 
{ 
    echo $question->getField('Question').' - '. $question->getField('Answer').'<br />'; 
} 

我的输出

Has the noise around the surrounding are increased with the new store opening – Strongly Agree 
Has the noise around the surrounding are increased with the new store opening – Strongly Agree 
Has the noise around the surrounding are increased with the new store opening – Agree 
Has the noise around the surrounding are increased with the new store opening – Disagree 
Has the noise around the surrounding are increased with the new store opening – Strongly Disagree 
Do you think the store closing earlier at weekend would help with noise levels - Strongly Disagree 
Do you think the store closing earlier at weekend would help with noise levels - Strongly Disagree 
Do you think the store closing earlier at weekend would help with noise levels - Strongly Agree 
Do you think the store closing earlier at weekend would help with noise levels - Strongly Disagree 
Do you think the store closing earlier at weekend would help with noise levels – Disagree 

我现在需要整理这些数据,无论是作为一个数组或JSON格式。我试图使用下面的格式,因为它看起来最简单。

[Question][StronglyAgree][Agree][Disagree][StronglyDisagree] 
[Has the noise around the surrounding are increased with the new store opening][2][1][1][2] 
[Do you think the store closing earlier at weekend would help with noise levels][1][0][1][3] 
Etc…. 

我会做一个的FileMaker脚本的方法是进行循环虽然$ data数组,在当前指针到最后一个问题(最后一个指针)比较的问题。如果它的不同将问题的值放在问题数组变量中。这将获得我所有的独特问题。看看PHP文档,我发现array_unique会为我做所有这些,但我不能让它与我的foreach($ data as $ key => $ question)一起工作。

一旦我有我的唯一的问题,我会再循环虽然$数据对我在计算所有的非常同意,然后把该数值到第一[]针对问题查找问题又来了比较。再次,我会循环3其他每个问题的同意,不同意...

有没有人知道的教程或答案,这将帮助我结合这些结果与问题的答案计数。不要太费心左右格式,像下面将工作

Has the noise around the surrounding are increased with the new store opening,2,1,1,2 
Or 
{“Question":"John", “Strongly Agree”:2,“Agree”:1,“Disagree”:1,“Strongly Disagree”:2 }, 

作为最后的手段,我想查询数据库4次,每次问题,但是这将是太多的电话,并杀死performance.Storing以某种方式将返回的值转换为格式化的变量。听起来有点过分,但可能最终的方法可能会比循环更容易理解。

回答

1

你为什么不使用的问题作为阵列的关键?

/* 
* @var array $surveyResults: [ 
* "My Question" => [ 
*  "Question" => "My Question" , 
*  "Strongly Agree" => 2, 
*  ... etc ... 
* ] 
* ] 
*/ 
$surveyResults = []; 
foreach ($data as $key => $question) { 
    // if we have not processed this question before, add it to the survey results 
    if (!isset($surveyResults[$question->getField('Question')])) { 
     $surveyResults[$question->getField('Question')] = [ 
      "Question"   => $question->getField('Question'), 
      "Strongly Agree" => 0, 
      "Agree"    => 0, 
      "Disagree"   => 0, 
      "Strongly Disagree" => 0 
     ]; 
    } 
    // count answer 
    $surveyResults[$question->getField('Question')][$question->getField('Answer')]++: 
} 

echo json_encode($surveyResults); 

这应该给你喜欢的结果。

+0

谢谢乔乔与您的代码回复。当你这样提及时,总是有意义的,知道我不需要拨打这么多的电话给db,但是缺乏经验。我刚刚试过你的代码,我似乎遇到了产生未定义索引错误的json_encode行的问题。评论说,行,并做了print_r($ surveyResults),看看它是如何填充的,并产生了预期的结果:)我需要做更多的阅读json_encode并弄清楚发生了什么事情。 – JK36