0

var_dump of my array:

   array(10) { ["idcomment"]=> string(2) "26" [0]=> string(2) "26" ["parentcomment_id"]=> string(2) "25" [1]=> string(2) "25" ["comment"]=> string(531) "damnJoin Dev Center and publish your app in the Windows Phone Store. GET SDK GET SDK Download the tools to build great Windows Phone apps. VIEW SAMPLES VIEW SAMPLES View code samples from Microsoft and the community to get started. Develop games for Windows PhoneThere has never been a better time to develop games for Windows Phone. We’ve made it simple for you to get started with support for native code and in-app purchase. One third of all Windows Phone downloads and 60 percent of revenue are from games – get started now." [2]=> string(531) "damnJoin Dev Center and publish your app in the Windows Phone Store. GET SDK GET SDK Download the tools to build great Windows Phone apps. VIEW SAMPLES VIEW SAMPLES View code samples from Microsoft and the community to get started. Develop games for Windows PhoneThere has never been a better time to develop games for Windows Phone. We’ve made it simple for you to get started with support for native code and in-app purchase. One third of all Windows Phone downloads and 60 percent of revenue are from games – get started now." ["dt"]=> string(19) "2013-04-29 21:36:29" [3]=> string(19) "2013-04-29 21:36:29" ["iduser"]=> string(1) "1" [4]=> string(1) "1" } 

Here is resulting json_encode($comment) :

{"idcomment":"26","0":"26","parentcomment_id":"25","1":"25","comment":null,"2":null,"dt":"2013-04-29 21:36:29","3":"2013-04-29 21:36:29","iduser":"1","4":"1"} 

I am missing comment field. It works for shorter strings... What I am doing wrong?

Andrew
  • 7,619
  • 13
  • 63
  • 117

2 Answers2

3

You have a single qoutes in your comments section first escape it using str_replace like this

$newstring =str_replace('\'', '\\\'', $myString);

then do json_encode

json_encode($newstring, true);
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
3

Most likely, your string is not correctly encoded using UTF-8, which json_encode expects. See UTF-8 all the way through for more information.

If you have existing data encoded using some other encoding (e.g. Windows-1252), use a function such as mb_convert_encoding to convert it to UTF-8 prior to JSON encoding.

Community
  • 1
  • 1
PleaseStand
  • 31,641
  • 6
  • 68
  • 95