doumixiang2227 2019-05-07 08:18
浏览 119

如何将数组php转换为javascript数组

I have some problem about parsing array php to javascript array, I use json_encode but I get error like

Uncaught SyntaxError: Unexpected number

This my code :

<?php 
        $b=array();
        $a=array();
        $pat = 0;
        for($i = 0; $i<15;$i++)
        {
            if($pat == 5)
            {
                ini_set('memory_limit', '-1');
                array_push($b,$a);
                $a = array();
                $pat = 0;
            }
            ini_set('memory_limit', '-1');
            array_push($a,$i.'-1');
            $pat = $pat +1;
        }

    ?>
$(document).ready(function() {

        $('#example').DataTable( {
            serverSide: true,
            ordering: false,
            searching: false,

            ajax: function ( data, callback ) {
                var out1 = "<?php echo json_encode($b) ?>";
                var out = [["0-1","0-2","0-3","0-4","0-5"],["1-1","1-2","1-3","1-4","1-5"]];

                console.log(out);

                setTimeout( function () {
                    callback( {
                        // draw: data.draw,
                        data: out,
                        recordsTotal: 5000000,
                        recordsFiltered: 5000000
                    } );
                });
            },

        } );
    } );

I want result like variable out, what can I do to fix this bug thanks, and I'm sorry for my bad English

  • 写回答

1条回答 默认 最新

  • dongxin0031 2019-05-07 08:52
    关注

    use this code for reference as I mentioned you just need to json_encode for this

    Generally we use rest apis for this kind of work but you can use given code snip in picture for your reference. If you have any specific question, let me know

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)