简体   繁体   English

json_encode将php数组传递给javascript数组

[英]json_encode pass php array to javascript array

I have a more dimensional php array that i want to pass to a javascript array. 我有一个更二维的php数组,我想传递给javascript数组。

This is the array: 这是数组:

 $this->_db_list_arrray[$this->getID()][$key] = $row; 

its like "16":[[{"article_no_internal":"9987213"}]] and so on. 就像“ 16”:[[{{article_no_internal“:” 9987213“}]]]等等。 I encode it like this: 我这样编码:

 $shipping_part_list_array = json_encode($db_obj->getArticleList($elements)); 

and in javascript 和在JavaScript

 alert("<?php echo $shipping_part_list_array; ?>"); 

but the alert only shows []. 但警报仅显示[]。

Is there a better way to pass php array to java script array? 有没有更好的方法将php数组传递给Java脚本数组?

array(1) {
  [16]=>
  array(2) {
    [0]=>
    array(1) {
      [0]=>
      array(2) {
        ["article_no_internal"]=>
        string(6) "999184"
        ["article_name_internal"]=>
        string(29) "Geschenkbox Kerzenschein 2011"
      }
    }
    [1]=>
    array(1) {
      [0]=>
      array(2) {
        ["article_no_internal"]=>
        string(6) "999184"
        ["article_name_internal"]=>
        string(29) "Geschenkbox Kerzenschein 2011"
      }
    }
  }
}

this is in my console, now i need to parse to get the right data. 这是在我的控制台中,现在我需要解析以获得正确的数据。 Thank you 谢谢

您需要添加单引号来提醒您的JSON字符串:

alert('<?php  echo $shipping_part_list_array; ?>');

You should not put double quotes around the JSON encoded value; 您不应该在JSON编码值两边加上双引号; just the following will do: 只需执行以下操作即可:

alert(<?php echo $shipping_part_list_array; ?>);

Though, for debugging purposes the following would be better: 但是,出于调试目的,以下方法会更好:

console.log(<?php echo $shipping_part_list_array; ?>);

Lastly, to assign it to a JavaScript variable: 最后,将其分配给JavaScript变量:

var list = <?php echo $shipping_part_list_array; ?>;

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

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