2

Here is my code:

$json = '{"1":{"show":true},"2":{"show":false}}';
echo '<pre>',print_r(json_decode($json, true)),'</pre>';exit;

Following is the output of the code.

enter image description here

How can I preserve the boolean values? Thanks in advance.

vkj
  • 537
  • 4
  • 11
  • 8
    That actually is how true/false is often interpreted. Just roll with it. – aynber Mar 09 '18 at 14:10
  • 2
    Everything is preserved. – u_mulder Mar 09 '18 at 14:11
  • 6
    Nothing to do with `json_decode` that's `print_r`'s doing ... try `var_dump` instead. – CD001 Mar 09 '18 at 14:12
  • This is how [`print_r()`](http://php.net/manual/en/function.print-r.php) displays boolean values. You can use [`var_dump()`](http://php.net/manual/en/function.var-dump.php) to get a more accurate representation. – axiac Mar 09 '18 at 14:13
  • 1
    And just because I imagine this might come up - the reason you've got a **1** at the end of the output in screenshot is because you're echo-ing the result of the `print_r` call – iainn Mar 09 '18 at 14:14
  • To add to @iainn 's comment, you would add a `,true` to the `print_r` for concat echo use, to have it return the content instead of the bool result. – IncredibleHat Mar 09 '18 at 14:53
  • 1
    Need a flag to close on "Thats how it works... nothing wrong here, move along" ... – IncredibleHat Mar 09 '18 at 14:54

0 Answers0