1

I have a PHP array looking like:

$columnDefs = array();

$column = new StdClass();
$column->target = array(0);
$column->orderable = false;
$column->searchable = false;
$column->render = 'function(id){
    return \'<input type="checkbox" value="\' + id + \'" />\';
}';

$columnDefs[] = $coluna;

I want to encode it with some kind of JSON to use it in JavaScript. The result should look like this:

[
    {
        "target": [0],
        "orderable": false,
        "searchable": false,
        "render": function(id){
            return '<input type="checkbox" value="' + id + '" />';
        }
    }
]

The problem is the json_encode() PHP function is quoting the "render" field. Is there a way to avoid this?

fonini
  • 3,243
  • 5
  • 30
  • 53
  • 5
    No. You are assigning a string, so you are getting a string. `function() {...}` is not valid JSON. – Felix Kling May 07 '15 at 17:49
  • @FelixKling is correct, but I feel compelled to add that your example is a valid JavaScript Object. The distinction should be made that "JSON (JavaScript Object Notation) is a lightweight data-interchange format" that can be read and written by multiple languages. "It is based on a subset of the JavaScript Programming Language. JSON is a text format that is completely language independent." (src: [JSON spec](http://www.json.org/)) – gfullam May 07 '15 at 18:09

0 Answers0