Monday, March 28, 2011

php json_encode returning null if empty

If i am converting an empty array into json using json_encode it is comming as null object.

My output was :

Networkcheckins":[null]

To avoid that we can use this :

/*** create an object ***/
$checkinObjNew = new stdClass;
$checkinObj = array();
$encodedArray = array_map(utf8_encode, $checkinObj);
$checkinObjNew->Networkcheckins = $encodedArray;
print_r(json_encode($checkinObjNew));


Now it will return :
Networkcheckins":[]

No comments:

Post a Comment