Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php数组到javascript对象_Javascript_Php_Arrays - Fatal编程技术网

php数组到javascript对象

php数组到javascript对象,javascript,php,arrays,Javascript,Php,Arrays,嗨,我在把值从php数组添加到javascript对象时遇到了一个问题。到目前为止,我在数组中有一个php值 $data=array(5,10,15,20); 我将其转换为json以准备javascript $js_array = json_encode($data); 这里是javascipt部分 数据: 我想它是在读它 data : [5] or data : [5101520] 应该把它读作 data : [5,10,15,20] 谢谢你们希望你们能帮助我 下面是php数组 <

嗨,我在把值从php数组添加到javascript对象时遇到了一个问题。到目前为止,我在数组中有一个php值

$data=array(5,10,15,20);
我将其转换为json以准备javascript

$js_array = json_encode($data);
这里是javascipt部分

数据:

我想它是在读它

data : [5] or data : [5101520]
应该把它读作

data : [5,10,15,20]
谢谢你们希望你们能帮助我

下面是php数组

<?php  
$data = array();

$que = "SELECT name FROM table1 ORDER BY date ASC";

$res = mysql_query($que, $con);

if(mysql_num_rows($res)>0){

while($row = mysql_fetch_array($res)){

$data[] = $row['name'];

}}

$js_array = json_encode($data);

?>'
'
这是var转储数据回音 数组(4){[0]=>string(1)“5”=>string(2)“10”[2]=>string(2)“20”[3]=>string(2)“15”}

php

js与jquery:

$.getJSON( "script.php", function( data ) {
console.log(data)
}

您定义php数组的方式将抛出语法错误

这样使用:

<?php
$data=array(5,10,15,20);
print_r(json_encode($data));
?>
以下是演示:


现在您可以在javascript中使用这个json对象了。

我不认为
$data()=[5,10,15,20]
是有效的php吗?
$data()=[5,10,15,20]不是有效的PHP。很近。请显示您的实际输出,而不仅仅是您认为它是什么。php数组中的数据很好,我使用php中的echo进行了检查抱歉,如果我输入了错误的语法,但我只想在php中显示值array@JoseSamaniego不,这是无效的PHP:@TomChew headMillard有一个新的括号语法:
[1,2,3]
vs.
数组(1,2,3)
。就这些。顺便说一下,
[1,2,3,4,5]
是非常有效的语法。只有括号是问题所在。
<?php
$data=array(5,10,15,20);
print_r(json_encode($data));
?>
data : [5,10,15,20]