How to pass null value via json_encode

1

How can I pass a json from php to jquery with null values. I have the structure below:

$data1 = null; $data2 = null; $data3 = null;
json_encode(array(
    'html'=>$html, 
    'data1'=>$data1, 
    'data2'=>$data2, 
    'data3'=>$data3)
);

Passing null gives an error:

  

Uncaught SyntaxError: Unexpected token < in JSON at position 0

PHP usage 5.5.12

    
asked by anonymous 30.05.2016 / 19:10

1 answer

0

You are passing the null value correctly. The problem I see in your code is that you wrote $date3 in json_encode() instead of $data3 . Otherwise it is not saving the json_encode return. Solving this, the code worked perfectly here.

Solution:

$data1 = null;
$data2 = null;
$data3 = null;

$json = json_encode(array(
  'html' => $html,
  'data1' => $data1,
  'data2' => $data2,
  'data3' => $data3
));;
    
30.05.2016 / 19:31