Json_encode array with special character

I have this array

array (size=3)
  0 => 
    array (size=4)
      'lat' => string 'qqq' (length=11)
      'lng' => string 'qqq' (length=11)
      'housenumber' => string 'xxx' (length=3)
      'street' => string 'José Ellauri' (length=12)
  1 => 
    array (size=4)
      'lat' => string 'qqq' (length=11)
      'lng' => string 'qqq' (length=11)
      'housenumber' => string 'xxx' (length=4)
      'street' => string 'Francisco Solano García' (length=23)
  2 => 
    array (size=4)
      'lat' => string 'qqq' (length=11)
      'lng' => string 'qqq' (length=11)
      'housenumber' => string 'xxx' (length=3)
      'street' => string 'Ingeniero Carlos María Maggiolo' (length=31)

      

I am trying json_encode

this array, but since there are special characters, I found out what I need. $toReturn = array_map('utf8_encode', $toReturn);

But I am getting an error. My code is below.

$toReturn = array_map('utf8_encode', $toReturn);
echo json_encode($toReturn);

      

I am getting this error on my page.

(!) Warning: utf8_encode () expects parameter 1 to be a string, array given in C: \ wamp \ www \ resh \ backend.php on line 39

+3


source to share


1 answer


This is because it array_map()

will transmit data containing an array. Try with



$toReturn = array_map('encode_all_strings', $toReturn);

function encode_all_strings($arr) {
    foreach($arr as $key => $value) {
        $arr[$key] = utf8_encode($value);
    }
    return $arr;
}

      

+3


source







All Articles