php - Why would json_encode return an empty string -


i have simple php structure 3 nested arrays.

i not use particular objects , build myself arrays 2 nested loops.

here sample of var_dump of array want convert json.

array (size=2)   'tram b' =>      array (size=2)       0 =>          array (size=3)           'name' => string 'ile verte' (length=9)           'distance' => int 298           'stationid' => int 762       1 =>          array (size=3)           'name' => string 'la tronche hôpital' (length=18)           'distance' => int 425           'stationid' => int 771   16 =>      array (size=4)       0 =>          array (size=3)           'name' => string 'bastille' (length=8)           'distance' => int 531           'stationid' => int 397       1 =>          array (size=3)           'name' => string 'xavier jouvin' (length=13)           'distance' => int 589           'stationid' => int 438 

in script have similar structure , json_encode works fine. don't understand why json_encode won't work here.

edit : there seems problem encoding. when mb_detect_encoding returns ascii, json_encode works when returns utf8, doesn't work anymore.

edit2 : json_last_error() returns json_error_utf8 means : malformed utf-8 characters, possibly incorrectly encoded.

well after 2 hours of digging (cf edits)

i found out following :

  • in case it's encoding problem
  • mb_detect_encoding returns faulty response, strings not utf-8
  • using utf8_encode() on string solved problem.

here recursive function can force convert utf-8 strings contained in array:

function utf8ize($d) {     if (is_array($d)) {         foreach ($d $k => $v) {             $d[$k] = utf8ize($v);         }     } else if (is_string ($d)) {         return utf8_encode($d);     }     return $d; } 

use this:

echo json_encode(utf8ize($data)); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo