1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

copy 2d php array to 2d javascript array

Discussion in 'JavaScript' started by Hemant Agarwal, Oct 2, 2010.

  1. #1
    Hi,
    Here is a working code to copy 2d php array to 2d javascript array.

    
    
    <html>
    
    <head>
    
    <?php
    
    for($i = 0; $i < 3; $i++)
     {
       for($j = 0; $j < 2; $j++)
                       {$quest[$i][$j] = $i*10+$j;}
     }
    
    
    ?>
    
    
    
    <script type="text/javascript">
    
    
    var questions = new Array(3);
    
      for (var i = 0; i < 3; i++)
      {
       questions[i] = new Array(2);
      }
    
    questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"];
    questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"];
    questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"];
    
    
    
    document.write(questions[0][0] + "<br />");
    document.write(questions[0][1] + "<br />");
    document.write(questions[1][0] + "<br />");
    document.write(questions[1][1] + "<br />");
    document.write(questions[2][0] + "<br />");
    document.write(questions[2][1] + "<br />");
    
    
    </script>
    
    
    
    
    </head>
    
    </html>
    
    
    
    Code (markup):





    Now,here's the thing.Notice these lines in the code


    questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"];
    questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"];
    questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"];

    I would like to put these lines in a loop,something like

    for (var i = 0; i < 3; i++)
    {
    questions = ["<?php echo join("\", \"", $quest); ?>"];
    }

    But even after a lot of efforts I am unable to do so,what am I doing wrong?Thanks
     
    Hemant Agarwal, Oct 2, 2010 IP
  2. clockedout7

    clockedout7 Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Indeed, because this is impossible. PHP runs on the server, while javascript runs in the browser. Since they run in totally different environments, there's no way to directly exchange information between them (which is what you would need to make a loop like that).
     
    clockedout7, Oct 4, 2010 IP
  3. Hemant Agarwal

    Hemant Agarwal Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    But,the thing is I am able to successfully store the php array in the javascript array using the following:

    
    questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"];
    questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"];
    questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"];
    
    Code (markup):
    I just need to put the above lines in a loop.
     
    Hemant Agarwal, Oct 4, 2010 IP
  4. clockedout7

    clockedout7 Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you want to do something like your first example, you need to do it in PHP (PHP generating javascript), not javascript, so try something like this (try changing the $count variable):

    
    <html>
    
    <head>
    
    <?php
    
    $count=3;
    
    for($i = 0; $i < $count; $i++)
     {
       for($j = 0; $j < 2; $j++)
                       {$quest[$i][$j] = $i*10+$j;}
     }
    
    ?>
    
    <?php
    	echo '<script type="text/javascript">';
    
    	echo "var questions = new Array($count);";
    
    	echo "for(var i = 0; i < $count; i++)";
    	echo '{';
    	echo '	questions[i] = new Array(2);';
    	echo '}';
    
    	for($i=0; $i<$count; $i+=1)
    	{
    		echo "questions[$i] = [\"" . join("\", \"", $quest[$i]) . "\"];";
    	}
    
    	for($i=0; $i<$count; $i+=1)
    	{
    		echo "document.write(questions[$i][0] + \"<br />\");";
    		echo "document.write(questions[$i][1] + \"<br />\");";
    	}
    
    	echo '</script>';
    ?>
    
    </head>
    
    </html>
    
    
    Code (markup):
     
    clockedout7, Oct 5, 2010 IP
  5. Hemant Agarwal

    Hemant Agarwal Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #5
    Hi,
    I would like to confirm that the method suggest above by clockedout7 indeed works.I was looking for the answer to this question for a long time.Thanks a lot clockedout7.

    By the way,a suggestion/request to the moderators of the forum.There should be a "thread resolved" button which people can click if their query is solved.This will help the very kind and helpful problem solvers from unnecessary reading threads that have been resolved
     
    Last edited: Oct 8, 2010
    Hemant Agarwal, Oct 8, 2010 IP