用php生成javascript代码

I would like to dynamically generate the columns definition on a Datatable. The columns definitions are:

"columns": [
            { "data": "id", "orderable": false },
            { "data": "code" },
            { "data": "name" },
            { "data": "created", "render":
                    function (data) {
                        var date = new Date(data);

                        return date.toLocaleString();
                    }
            },
            { "data": "modified", "render":
                    function (data) {
                        var date = new Date(data);

                        return date.toLocaleString();
                    }
            }]

I tried generating the javascript code using an array of php objects and then json_encode it, like the following:

$formatted_cols = [];

foreach ($cols as $idx => $col){
    $temp = [];

    $temp['data'] = $col;

    if(in_array($col, array('id', 'actions'))){
        $temp['orderable'] = 'false';
    }

    if(in_array($col, array('created', 'modified'))){
        $temp['render'] = "
            function (data) {
                var date = new Date(data);

                return date.toLocaleString();
            }
        ";
    }

    $formatted_cols[] = $temp;
}

And then I do the following in the place where the code would normally appear:

echo json_encode($formatted_cols);

But the code came out like this:

[
  {
    "data": "id",
    "orderable": "false"
  },
  {
    "data": "code"
  },
  {
    "data": "name"
  },
  {
    "data": "created",
    "render": "
            function (data) {
                var date 
= new Date(data);
    
                return 
date.toLocaleString();
            }
        "
  },
  {
    "data": "modified",
    "render": "
            function (data) {
                var date 
= new Date(data);
    
                return date.toLocaleString();
            }
        "
   }
]

As you can see, with a bunch of and stuff. Anybody can help me get the desired output please?

Thanks in advance for any help

UPDATE

I removed the line breaks but still the functions are inside double quotes

{
    "data": "modified",
    "render": "function (data) {var date = new Date(data);return 
    date.toLocaleString();}"
  }

How do I remove those quotes?

Remove your newlines, like this:

{ "data": "created", "render": "function (data) { var date = new Date(data); return date.toLocaleString(); }"  },

But you'll still be left with a string, which isn't the sort of thing you should work with, even though you can convert it to a function in JS. It's pretty ugly. Even if it worked, it's not great to generate JS in PHP - try to find another method if you can. Let PHP serve only the data, and have JS integrate functionality into it, if possible.

The "stuff" represents a carriage-return + linefeed combination, in other words a line break.

You're going to get these in the JSON data if you're trying to encode multi-line strings. JSON itself does not support multi-line strings without them like PHP does.

Try using nl2br(). It'll take away all those and

http://php.net/manual/es/function.nl2br.php