将 PHP 数组复制到 javascript 数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14973654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:03:09  来源:igfitidea点击:

Copy PHP array into javascript array

phpjavascript

提问by Tony Stark

I want to copy a PHP array into javascript array. The javascript is being used in the PHP file. It is echoed in the PHP code so when i tried to create a new array in javascripy and just assign the PHP array onto it, it was giving me an error. Array to string conversion. Could some one help me with this.

我想将一个 PHP 数组复制到 javascript 数组中。javascript 正在 PHP 文件中使用。它在 PHP 代码中得到了回应,所以当我尝试在 javascripy 中创建一个新数组并将 PHP 数组分配给它时,它给了我一个错误。数组到字符串的转换。有人可以帮我解决这个问题。

 mysql_select_db("cerebra", $con);
 $sql="select name from details order by download desc limit 20";
 if (!mysql_query($sql,$con))`enter code here`
  {
  die('Error: ' . mysql_error());
  }
 $query=mysql_query($sql,$con);
 $names=array();
 while($row=mysql_fetch_array($query))
   {
$names[]=$row[0];
   }

Here $names is the array i want to use in javascript

这里 $names 是我想在 javascript 中使用的数组

then i echo my javascript inside the php code itself..:

然后我在 php 代码本身中回显我的 javascript..:

   function makeRequest() {
   var d = document.getElementsByName(\"d\")[0].value;
   var request = gapi.client.drive.files.list();
var newnames = new Array();
    newnames=$names;
  request.execute(function(resp)

 {        
    for (i=0; i<resp.items.length; i++) {
        var titulo = resp.items[i].title;
        var fechaUpd = resp.items[i].modifiedDate;
        var userUpd = resp.items[i].lastModifyingUserName;
        var userEmbed = resp.items[i].embedLink;
        var userAltLink = resp.items[i].alternateLink;
        var download = resp.items[i].webContentLink;
        var hold=\"Download\";
        var flag=0;

         <!-- var fileInfo = document.createElement('li');
      <!--  fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd +'  url:  ' + hold.link(download)));                
       <!-- document.getElementById('content').appendChild(fileInfo);
        <!--for (var i=0;i<newnames.length;i++){
    <!--    if(newnames[i].toString() == titulo){
        document.write(titulo + \"&nbsp;\");
        document.write(hold.link(download) + \"<br>\");
        <!--flag=1;
    <!--    }
        <!--}
        }
        <!--if(flag!=1){
        <!--document.write(\"not found!\");
        <!--}
});    
}

Here in the javascript i am trying to convert the php array into javascript array but its not working

在 javascript 中,我试图将 php 数组转换为 javascript 数组,但它不起作用

回答by Tony Stark

Example-1:

示例 1:

<script type='text/javascript'>
<?php
$php_array = array('he','hi','hello');
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>

NOTE:You can convert php arrays into javascript using php's json_encode()function.

注意:您可以使用 php 的json_encode()函数将 php 数组转换为 javascript 。

Check out this convert php array to java script arrayfor more information.

查看此将 php 数组转换为 java 脚本数组以获取更多信息。

Example-2:

示例 2:

PHP Code:

PHP代码:

$arr = array(1,2,3,4,5,6,7);
$script = '<script>var newArr = new Array(' . implode(',', $arr) . ');</script>';
echo $script; 

HTML Code:

HTML代码:

<script>
function doOnchange(arr){
    alert(arr.length);
}
</script>
<form name="frm">
    <select name="sel1" onchange="doOnchange(newArr);">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
</form>

Checkout some more links,

查看更多链接,

How to Convert PHP Multidimensional Array to Javascript Object (using jQuery)

如何将 PHP 多维数组转换为 Javascript 对象(使用 jQuery)

array2json() - Convert PHP arrays to JSON

array2json() - 将 PHP 数组转换为 JSON

json_encode pass php array to javascript

json_encode 将 php 数组传递给 javascript

may this help you.

可能这对你有帮助。

回答by Imperative

Have a look at the json_encode()function. Modified example from the site:

看看json_encode()函数。来自网站的修改示例:

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo 'var js_object = '.json_encode($arr).';';
// outputs: var js_object = {"a":1,"b":2,"c":3,"d":4,"e":5};
?>

回答by Samy

If its a single or simple array you can manually construct the javascript array from PHP array. But its not a good way. I would suggest the json or xml way.

如果它是单个或简单的数组,您可以从 PHP 数组手动构造 javascript 数组。但这不是一个好方法。我建议使用 json 或 xml 方式。

 <?php 
    $myArr = array(1,2,3,4,5);
  ?>
 <script>
  var myArr = new Array();
  <?php 
    for($i=0;$<count($myArr);$i++) { ?>
     myarr[<?php echo $i ?>] = <?php echo $myArr[$i] ?>;
    <?php } ?>
  ?>
 </script>