2017-10-09 72 views
0
UPDATE! ADDING PHP code 

<?php 
    $tq_array = array(); 
    $c_array = array(); 
    $o1_array = array(); 
    $o2_array = array(); 
    $o3_array = array(); 
    $questions = mysql_query("SELECT * FROM test WHERE exam_code = '$exam'"); 
     while($row = mysql_fetch_assoc($questions)){ 
     $tq_array[] = $row['test_question']; 
     $c_array[]= $row['correct_answer']; 
     $o1_array[] = $row['option_1']; 
     $o2_array[] = $row['option_2']; 
     $o3_array[] = $row['option_3']; 
     } 
     $tq_json = json_encode($tq_array); 
     $c_json = json_encode($c_array); 
     $o1_json = json_encode($o1_array); 
     $o2_json = json_encode($o2_array); 
     $o3_json = json_encode($o3_array); 
?> 

我的問題是我想將這個轉換後的javascript數組(從php數組)添加到另一個存儲。在單個javascript數組中添加很多javascript數組

var questioner = JSON.parse('<?= $tq_json; ?>'); 
var correct_ans = JSON.parse('<?= $c_json; ?>'); 
var option1 = JSON.parse('<?= $o1_json; ?>'); 
var option2 = JSON.parse('<?= $o2_json; ?>'); 
var option3 = JSON.parse('<?= $o3_json; ?>'); 

這就是我要添加代碼

var i = 0 
for(i = 0; i <= obj1.length;i++){ 
    var questions = [ 
     [ /* this is the one that fix the counting of the questioners*/], 

     [[obj1[i], obj2[i], obj3[i], obj4[i], obj5[i]],"false",1], 



     ], 

    sec=20, 
    A; 
} 

我嘗試循環使用循環,但它沒有工作的存儲。

我喜歡出現像這樣()

var questions = [ 
     [ /* this is the one that fix the questioner */], 

     [[obj1[i], obj1[i], obj1[i], obj1[i], obj1[i]],"false",1], 
     [[obj2[i], obj2[i], obj2[i], obj2[i], obj2[i]],"false",1], 
     [[obj3[i], obj3[i], obj4[i], obj3[i], obj3[i]],"false",1], 

      So on .... 


     ], 

    sec=20, 
    A; 

謝謝!

+3

在你的代碼示例你的變量不匹配了,我們不知道的PHP陣列的內容。這使得很難確切地看到你的意思。你能否更新這個。 – Mouser

回答

0

您正在通過obj1陣列爲每次迭代構建一個新陣列。我們需要促進questions陣列的範圍和推到它:

var i = 0 
var questions = []; 
for(i = 0; i <= obj1.length;i++){ 
    questions.push(
     [[obj1[i], obj2[i], obj3[i], obj4[i], obj5[i]],"false",1], 
    ); 
} 
+0

我得到了一個空白的結果先生。 –

+0

您需要調試和/或使用'console.log(問題)'來查看問題數組 –

+0

的內容。好吧,先生,盡我所能去調試。生病告訴你後來的結果 –

0

我想你想創建一個多維數組也許更難讀哪個,所以我建議你使用對象來代替。

但這裏的答案應該幫助 JavaScript multidimensional array

+0

我想我不會使用多元化的先生。即時通訊使用行數組。 –