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
  • 184,866
  • 48
  • 414
  • 792
cube
  • 1,724
  • 3
  • 23
  • 32

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
  • 30,290
  • 9
  • 117
  • 125
mario
  • 141,508
  • 20
  • 234
  • 284
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
  • 9,687
  • 6
  • 54
  • 105
leon.clements
  • 95
  • 1
  • 6