2

This is my $var from json_encode:

{
"key1":"\u0000data1",
"key2":"\u0000data2",
"key3":"\u0000data3",
"key4":"\u0000data4
}

I would like to do this:

echo json_encode(str_replace ("\\u0000", "",  $var));

in order to get rid of the preceding \u0000 that's showing up, the line above doesn't work to strip it.

hakre
  • 193,403
  • 52
  • 435
  • 836
cube
  • 1,774
  • 4
  • 23
  • 33

2 Answers2

7

You'll have to apply the function the other way round:

echo str_replace('\\u0000', '', json_encode($var));

This is because $var is an array. You'd have to iterate over all its entries and look for the \0 byte otherwise.

yckart
  • 32,460
  • 9
  • 122
  • 129
mario
  • 144,265
  • 20
  • 237
  • 291
0

I ran into something similar.

This question & answer helped me understand the root cause of the problem.

I overcame this by realising that my class properties (equivalent to each keyn in your question) didn't really need to be protected. By making them public I side-stepped the issue altogether.

I suppose it's up to you to decide if this is best practice or suited to your project however.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Leon Clements
  • 95
  • 1
  • 7