1

but could not find a solution.

so I have an Array that looks like this:

Array(
[0] => Array
    (
        [ID] => 1
        [Vorname] => Fisrtname
        [Nachname] => Lastname
        [Geburtsdatum] => 1990-01-01
        [Email] => [email protected]
        [Telefon] => 0511123123
    ))

I want to convert it to JSON and use it as response with Slim.

The problem ist, that echo json_encode(); and return $response->withJson(); returns nothing.

As I said, I serached a lot and this this two ways was all I could find. Maybe you know why this didn't work.

10
  • whats the exact code you are using? Commented Mar 27, 2018 at 21:56
  • 1
    json_last_error_msg() php.net/manual/en/function.json-last-error-msg.php Commented Mar 27, 2018 at 21:57
  • @ArtisticPhoenix thanks for this tipp. I got this message: Malformed UTF-8 characters, possibly incorrectly encoded Commented Mar 27, 2018 at 21:59
  • 1
    Loop through array and fix it, $item = mb_convert_encoding($item, "UTF-8", "UTF-8") Commented Mar 27, 2018 at 22:01
  • We cannot say what ` $response->withJson();` is or does, since you did not tell us. But we certainly can say that json_encode() does work when called with an array as argument. Commented Mar 27, 2018 at 22:01

2 Answers 2

5

RE: json_last_error_msg()

Malformed UTF-8 characters, possibly incorrectly encoded

It's a common issue.

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;
}

It's almost like I just copied this code from something I wrote before ... lol ... this give me lot of headaches in the past. So, been there done that.

Sign up to request clarification or add additional context in comments.

5 Comments

had to remove $this-> as this was from a class I use for Json Encoding that does some automatic error resolution things, so I functionized it for you.
I used PDO to get the Array. So an other way is this : 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ";charset=utf8"
I tried that, I forget why I commented it out in the original one though ... lol. So I can't remember why now, but I'm sure I had a reason. See //$fixed = utf8_encode(); Maybe it was just faster, we do billions of rows of data so I always compare speeds. Honestly I don't remember...
Maybe it was Windows-1252 which mb_ does convert... Please note that utf8_encode only converts a string encoded in ISO-8859-1 to UTF-8 In any case I tried it and decided not to use it...
@Adrian - that would be the preferred way if its a database source, in my case it's CSV files, that get put in a queuing system as JSON.
-1
<?php
$arr=[
  [
    'ID'           => 1,
    'Vorname'      => 'Fisrtname',
    'Nachname'     => 'Lastname',
    'Geburtsdatum' => '1990-01-01',
    'Email'        => '[email protected]',
    'Telefon'      => '0511123123'
  ]
];
echo json_encode($arr);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.