PHP JSON 和 json_encode 的数组无效

PHP Array to JSON with json_encode didnt work

但找不到解决方案。

所以我有一个如下所示的数组:

Array(
[0] => Array
    (
        [ID] => 1
        [Vorname] => Fisrtname
        [Nachname] => Lastname
        [Geburtsdatum] => 1990-01-01
        [Email] => test@testmail.com
        [Telefon] => 0511123123
    ))

我想将其转换为 JSON 并将其用作 Slim 的响应。

问题是 echo json_encode();和 return $response->withJson(); return没什么。

正如我所说,我搜索了很多,这两种方式都是我能找到的。也许你知道为什么这不起作用。

回复:json_last_error_msg()

Malformed UTF-8 characters, possibly incorrectly encoded

这是一个常见问题。

function utf8convert($mixed, $key = null)
{
    if (is_array($mixed)) {
        foreach ($mixed as $key => $value) {
            $mixed[$key] = utf8convert($value, $key); //recursive
        }
    } elseif (is_string($mixed)) {
        $fixed = mb_convert_encoding($mixed, "UTF-8", "UTF-8");
        return $fixed;
    }
    return $mixed;
}

这几乎就像我只是从我以前写的东西中复制这段代码...大声笑...这在过去让我很头疼。所以,在那里做到了。

<?php
$arr=[
  [
    'ID'           => 1,
    'Vorname'      => 'Fisrtname',
    'Nachname'     => 'Lastname',
    'Geburtsdatum' => '1990-01-01',
    'Email'        => 'test@testmail.com',
    'Telefon'      => '0511123123'
  ]
];
echo json_encode($arr);
?>