简体   繁体   English

在Javascript循环内遍历PHP数组

[英]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... 我在尝试遍历Javascript数组中的PHP数组时遇到了一些麻烦...我在论坛上进行了搜索,尽管我发现了一些有关将PHP值复制到Javascript值的帖子,但我无法找到确切的内容我正在努力实现...

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"). 我有一个名为“ phpArray”的PHP数组,我希望将此数组的每个值复制到Javascript数组(称为“ 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". 我有一个Javascript循环,当“ phpArray”为空时填充Javascript数组,而我只是在尝试使用PHP索引来迭代“ 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: 但是,它的作用就好像从未增加PHP索引一样,并且我唯一能得到的数组值是“ phpArray”的第一个数组……这是与之对应的一段代码:

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! 在此先感谢您的时间和精力! :) :)

您应该使用json_encode

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

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. 根据评论(为什么不使用JSON编码?),JS和PHP之间的连接仅是单向的,因此您需要在PHP中创建完整的JS代码。

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. 所有JS代码都需要由PHP输出。

As suggested in the comments, this will work only if the javascript is generated from a .php page. 如评论中所建议,仅当从.php页面生成javascript时,此方法才有效。 If it's a .js script, it won't work. 如果是.js脚本,它将无法正常工作。

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. 您的代码无法正常工作的原因是您有一个JavaScript周期,而不是一个PHP周期。 In PHP, you could do this: 在PHP中,您可以这样做:

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] . ";";
     }
  }
?>

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. 我没有大量使用PHP,但是我怀疑大多数人会使用JSON函数或库来创建分配给Javascript变量的文本-您应该调查一下。

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. 在您的情况下,我可以看到您正在尝试做的事情,但是当您使用时,绝对必须考虑将PHP /服务器端写为javascript端脚本。 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. 您不能混合使用两种语言,因为无法通过JavaScript i和j将PHP $ i和$ j保留在kilter中。

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. 要澄清的是,在$ phpArray和$ i和$ j在服务器上超出作用域之后,很长一段时间以来,javascript的i和j就进入了客户端计算机的作用域-“永远不会见面”等。

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. 您可以使用PHP编写循环代码,并使用Javascript编写代码。 There will be no loop on the javascript side - just an 'unrolled' set of values. 在javascript端将没有循环-只是一组“展开”的值。

eg if i and j go from 0 to 2, with digits 0 to 8, you'd write PHP code to output the following: 例如,如果i和j从0到2,数字从0到8,您将编写PHP代码以输出以下内容:

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. 再次注意,在初始化期间,javascript没有循环-这些循环在PHP端。 Once you have the data down though, you can loop over it or index into it using javascript in the browser (but not PHP). 不过,一旦数据丢失,您就可以在浏览器中使用javascript(而不是PHP)对其进行循环或索引。

Hope that this helps. 希望这会有所帮助。

Mark ia.uk.com 马克ia.uk.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM