Johanovski
Johanovski

Reputation: 235

Iterating over a PHP array inside a Javascript loop

I'm having some troubles trying to iterate over a PHP array inside a Javascript array... I've searched over the forum and, though I found some posts about copying PHP values to Javascript values, I'm unable to find exactly what I'm trying to achieve...

I have a PHP array of arrays called "phpArray", and I want each value of this array to be copied into a Javascript array of arrays (called "javaArray"). I have a Javascript loop that populates the Javascript array when the "phpArray" comes empty, and I'm simply trying to use a PHP index to iterate over the "phpArray". However, it acts as if the PHP index is never increased, and the only array value that I can get is the first one of the "phpArray"... Here is the piece of code corresponding to this:

for (var i = 0; i < javaArray.length; i++) {
    javaArray[i] = new Array(<?php echo $numCols; ?>);

    for (var j = 0; j < javaArray[i].length; j++) {
        javaArray[i][j] = "0";

        <?php 
        if(sizeof($javaArray) > 0)
        {
            ?>
            javaArray[i][j] = "<?php echo $phpArray[$i][$j]; ?>";
            <?php
        }
        ?>
    }
}

Any idea on how can I do this?

Thanks in advance for your time and effort! :)

Upvotes: 1

Views: 4196

Answers (4)

Mark Rabjohn
Mark Rabjohn

Reputation: 1713

I don't do a great deal of PHP, but I would suspect that most folk would use a JSON function or library to create the text that assigns to a Javascript variable - you should look into that.

In your case, I can see what you are trying to do, but when you use you absolutely have to think of the PHP/server side as writing the script for the javascript side. You can't mix the two languages, because there's no way of keeping PHPs $i and $j in kilter with javascripts i and j.

To clarify, javascript's i and j come into scope on the client's machine long after $phpArray and $i and $j have gone out of scope on the server - 'never the twain shall meet' etc.

It looks like what you are trying to write is the array allocation and initialisation logic. There's no real problem with doing it this way for short loops. You code a loop in PHP, and write out code in Javascript. There will be no loop on the javascript side - just an 'unrolled' set of values.

e.g. if i and j go from 0 to 2, with digits 0 to 8, you'd write PHP code to output the following:

javaArray = new Array(2);
javaArray[0] = new Array(2);
javaArray[0][0] = 0;
javaArray[0][1] = 1;
javaArray[0][2] = 2;
javaArray[1] = new Array(2);
javaArray[1][0] = 3;
javaArray[1][1] = 4;
javaArray[1][2] = 5;
javaArray[2] = new Array(2);
javaArray[2][0] = 6;
javaArray[2][1] = 7;
javaArray[2][2] = 8;

Note again that during the initialisation, javascript does not have loops - those are on the PHP side. Once you have the data down though, you can loop over it or index into it using javascript in the browser (but not PHP).

Hope that this helps.

Mark ia.uk.com

Upvotes: 0

Mifeet
Mifeet

Reputation: 13608

As suggested in the comments, this will work only if the javascript is generated from a .php page. If it's a .js script, it won't work.

The easiest way is

var javaArray = <?php echo json_encode($phpArray) ?>;

as suggested by others.

The reason why you're code didn't work is that you have a javascript cycle, not a PHP cycle. In PHP, you could do this:

var javaArray = [];
<?php
  for ($i=0; $i < count($phpArray); $i++) {
     for ($j=0; $j < count($phpArray[$i]); $j++) {
         echo "javaArray[$i][$j] = " . $javaArray[$i][$j] . ";";
     }
  }
?>

Upvotes: 0

Voitcus
Voitcus

Reputation: 4446

According to comments (why not use JSON encoding?) the connection between JS and PHP is only one-directional, so you need to create complete JS code in PHP.

I propose doing something like (one dimensional array for clarity):

// this is PHP code
echo "var JavaArray = array("; // this echoes declaration of JavaScript array
foreach($phpArray as $item){ // this starts iterating PHP array
  echo $item.', '; // this "copies" PHP array item to JavaScript array item
}
echo ')'; // close JS declaration of array

This in fact is not perfect, as it leaves , on the ending but you get the idea. All JS code needs to be output by PHP.

Upvotes: 0

sroes
sroes

Reputation: 15053

You should use json_encode:

javaArray = <?php echo json_encode($phpArray) ?>;

Upvotes: 6

Related Questions