結合需要一些幫助,不斷來回的辦法可能做到這一點。我正在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以某種方式將返回的值轉換爲格式化的變量。聽起來有點過分,但可能最終的方法可能會比循環更容易理解。
謝謝喬喬與您的代碼回覆。當你這樣提及時,總是有意義的,知道我不需要撥打這麼多的電話給db,但是缺乏經驗。我剛剛試過你的代碼,我似乎遇到了產生未定義索引錯誤的json_encode行的問題。評論說,行,並做了print_r($ surveyResults),看看它是如何填充的,併產生了預期的結果:)我需要做更多的閱讀json_encode並弄清楚發生了什麼事情。 – JK36