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);
 ?>

my problem is i want to add this converted javascript array (from php array) to another storage.

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; ?>');

this is the storage where i want to add the codes

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;
}

i try to loop it using for loop but it didnt work.

i like to to appear it like this ()

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;

Thank you!

  • 3
    In your code samples your variables don't match up and we don't know the content of the php-arrays. This makes it very difficult to see exactly what you mean. Could you update this. – Mouser Oct 09 '17 at 11:32

2 Answers2

0

You're building a new array for each iteration through your obj1 array. We need to promote the scope of the questions array and push to it:

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],
    );
}
Dan D
  • 2,493
  • 15
  • 23
0

I think you're trying to create a multidimentional array which maybe harder to read, so I'd suggest you use objects instead.

But the answer here should help JavaScript multidimensional array

fortunee
  • 3,852
  • 2
  • 17
  • 29