如何将 php 数组值分配给 javascript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12813580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to assign php array values to javascript array
提问by KRA
Please provide help on how to assign php array values to javascript array
请提供有关如何将 php 数组值分配给 javascript 数组的帮助
----------- Using below php code I am storing data in php array -----------
----------- 使用下面的 php 代码我将数据存储在 php 数组中 -----------
<?php
$str_query="SELECT title,description FROM tablename ORDER BY title";
$rs_sch=GetRecordset($str_query);
$int_count=0;
$schd_arr = array();
while(!$rs_sch->EOF())
{
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");
$rs_sch->MoveNext();
$int_count++;
}
?>
----- Using below javascript code I am trying to store php array data into javascript array -----
----- 使用下面的 javascript 代码我试图将 php 数组数据存储到 javascript 数组中 -----
Please let me know what and how to write variable at below 2 mentioned locations so my code can work.
请让我知道在下面提到的 2 个位置写什么以及如何写变量,以便我的代码可以工作。
<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
{
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;
}
</script>
Thank you in advance, KRA
预先感谢您,KRA
回答by Hkachhia
You can directly use the json_encode
function. It is easy to use and produces valid javascript so is very stable:
您可以直接使用该json_encode
功能。它易于使用并生成有效的 javascript,因此非常稳定:
<script type="text/javascript">
var pausecontent = <?php echo json_encode($php_array); ?>;
</script>
回答by Mihai Iorga
You can't loop like that, you need to loop the PHP array and push
into javascript array:
你不能像那样循环,你需要将 PHP 数组循环push
到 javascript 数组中:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php foreach($schd_arr as $key => $val){ ?>
pausecontent.push('<?php echo $val; ?>');
<?php } ?>
</script>
回答by 11684
I think you'd best get the array to your javascript first. That would be something like this:
我认为你最好先把数组放到你的 javascript 中。那将是这样的:
var theVariableYouWantTheArrayIn = <?php echo json_encode($theArrayYouWantInJavascript); ?>
After that it is a normal js array, so you can use properties like .length which will presumably make the loop much easier.
之后它是一个普通的 js 数组,因此您可以使用 .length 之类的属性,这可能会使循环更容易。
回答by Ravi Kumar Yadav
I had the same problem doing this but finally found the best solution
我在这样做时遇到了同样的问题,但最终找到了最佳解决方案
<script>
var arr=[];
arr.push('<?php
include('conn.php');
$query = "SELECT *FROM c group by name";
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
while($data = mysqli_fetch_array($res))
{
echo $data["name"];
}
?>');
</script>
回答by Balaji Courier
Above both answers in good.
以上两个答案都很好。
- Iterate through the array you got as a result of your DB query and add each value to a new PHP variable,
- Add a counter and increment it during each iteration to make sure that you're not adding a comma after the last element,
- If the count of the elements in the array is more than 1, create the array as usual, otherwise create a new array with 1 element and assign your PHP array value to the index 0.
- 迭代通过数据库查询获得的数组,并将每个值添加到新的 PHP 变量中,
- 添加一个计数器并在每次迭代期间递增它以确保您没有在最后一个元素后添加逗号,
- 如果数组中的元素数大于 1,则照常创建数组,否则创建一个包含 1 个元素的新数组并将 PHP 数组值分配给索引 0。