Home > Backend Development > PHP Tutorial > js如何获取php的数组

js如何获取php的数组

WBOY
Release: 2016-06-13 13:35:17
Original
1016 people have browsed it

js怎么获取php的数组
php中存在省市对应数组
  $info = array(
  '北京'=>array('北京市'),
  '福建'=>array("福州市","龙岩市","南平市","宁德市","莆田市","泉州市","三明市","厦门市","漳州市"),
  '甘肃'=>array("白银市","定西市","甘南藏族自治州","嘉峪关","金昌市","酒泉市","兰州市","临夏回族自治州","陇南市","平凉市","庆阳市","天水市","武威市","张掖市"),
  '广东'=>array("潮州市","东莞市","佛山市","广州市","河源市","惠州市","江门市","揭阳市","茂名市","梅州市","清远市","汕头市","汕尾市","韶关市","深圳市","阳江市","云浮市","湛江市","肇庆市","中山市","珠海市"),);


怎么在前端js或者这个数组

------解决方案--------------------
转化成json格式
echo json_encode($info);

不知道你这是ajax还是? 如果不是,那就输出一段Js 代码好了 
<script><br />var jsondata = <?php echo json_encode($info); ?>;<br /></script>

如果是ajax ,那么JQuery的ajax json函数很好用。或者用eval("'(" + ajaxjsondata + ")'");什么的也很方便
------解决方案--------------------

HTML code


    $arr = "[[1,2,3],[2,3,4]]";
    echo $arr;
?>
<script>
    var tmp = '<?=$arr?>';
    tmp = eval('('+tmp+')')
    alert(tmp.length)
</script> <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template