skip to Main Content

90% of my website pages use the utf8 encoding feature for compile an DataTable.

$a[] = array_map('utf8_encode', $item);

With the old version 8.0 of php everything was fine, in the new version it gives me an error when a value of $item ($item is an array) is null.

What is a valid alternative?

3

Answers


  1. Chosen as BEST ANSWER

    I just do this before array_map('utf8_encode', $item):

    foreach(array_keys($item) as $key)
        $item[$key] = utf8_encode($item[$key] ?? '');
    

    and it works good.


  2. Use mb_convert_encoding to convert from ISO-8859-1 to UTF-8

    Example:

    $iso = "x5Ax6FxEB";
    echo mb_convert_encoding($iso, 'UTF-8', 'ISO-8859-1'); // Zoë
    
    Login or Signup to reply.
  3. We know that utf8_encode is deprecated since PHP 8.2.0 and will be removed in PHP 9 https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated

    So the alternative can be:

    $oldSample = ["x5Ax6FxEB"];
    $result= array_map
    (
        function ($item){
            return mb_convert_encoding($item, "UTF-8", mb_detect_encoding($item));
        }, 
        $oldSample
    );
    var_dump($result);
    

    Documentation:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search