2016-03-03 49 views
0

所以下面我有一個數組,我需要離開我需要輸出它在foreach循環抓取數據:陣列輸出的主要問題及asnwers

問題:如何使茶

答案: - 茶包 - 茶葉袋水牛奶糖

問:什麼是我的名字

答案: - 列維 - 馬克

陣列輸出:

array(2) { 
    [260]=> 
    object(Question)#16 (3) { 
    ["answers"]=> 
    array(2) { 
     [0]=> 
     object(Answer)#18 (5) { 
     ["id"]=> 
     string(3) "144" 
     ["answer"]=> 
     string(4) "levi" 
     ["questionId"]=> 
     string(3) "260" 
     ["correct"]=> 
     string(1) "0" 
     ["quizId"]=> 
     string(2) "33" 
     } 
     [1]=> 
     object(Answer)#19 (5) { 
     ["id"]=> 
     string(3) "143" 
     ["answer"]=> 
     string(4) "mark" 
     ["questionId"]=> 
     string(3) "260" 
     ["correct"]=> 
     string(1) "0" 
     ["quizId"]=> 
     string(2) "33" 
     } 
    } 
    ["id"]=> 
    string(3) "260" 
    ["question"]=> 
    string(15) "What is my name" 
    } 
    [259]=> 
    object(Question)#17 (3) { 
    ["answers"]=> 
    array(2) { 
     [0]=> 
     object(Answer)#20 (5) { 
     ["id"]=> 
     string(3) "142" 
     ["answer"]=> 
     string(7) "tea bag" 
     ["questionId"]=> 
     string(3) "259" 
     ["correct"]=> 
     string(1) "0" 
     ["quizId"]=> 
     string(2) "33" 
     } 
     [1]=> 
     object(Answer)#21 (5) { 
     ["id"]=> 
     string(3) "141" 
     ["answer"]=> 
     string(24) "tea bag water milk sugar" 
     ["questionId"]=> 
     string(3) "259" 
     ["correct"]=> 
     string(1) "0" 
     ["quizId"]=> 
     string(2) "33" 
     } 
    } 
    ["id"]=> 
    string(3) "259" 
    ["question"]=> 
    string(15) "How to make tea" 
    } 
} 

將是幫助

我開始感激,但不會不惜一切只是一個想法工作:

$questions = Quiz::OutputQuestions($id); 

foreach($questions as $question) { 
    echo 'Question: ' . $question['id']['question']; 
    echo 'Answers: <br />'; 

    foreach($answers as $answer) { 
      echo '- Answer <br />'; 
    } 
} 
+0

請問你能不能在這裏添加Array輸出字符串,而不是上傳圖片。 – ameenulla0007

回答

2

看來你有對象和而不是頂級數組$ question中的數組。所以你可以試試這個代碼:

$questions = Quiz::OutputQuestions($id); 

foreach($questions as $question) { 
    echo 'Question: ' . $question->question; 
    echo 'Answers: <br />'; 

    foreach($question->answers as $answer) { 
      echo '- ' . $answer->answer; 
    } 
    echo '<br />'; 
} 
+0

啊是的,這是我一直在尋找的謝謝 –