Json_encode and PHP 5.2.17

A server I push PHP scripts to is still running PHP 5.2.17 – its insanely old, almost 5 years!

I discovered that if I try to use that version of json_encode and pass it a string with unicode characters, I get nulls back where I should have UTF-8 encoded strings!  The code works fine on my local server, running PHP 7.0.15.

The fix for this is pretty simple, but worth noting for future reference:

 $text = utf8_encode($row[$key]);
 array_push($items,$text);

Where $row is an assoc. array and $key points to the value I want to push into my array which in turn is encoded with:

$json = json_encode($items);

Without the ‘utf8_encode’ I just get nulls in my encoded string 🙁

Leave a Reply