Good day. There was a problem: the json string is sent via the post and decrypted. Similarly, the same array is created, encrypted into the json string and also decoded. Externally, the lines are the same, but in the first case the line does not expand into an array. As it was possible to establish, extra 90 bytes are written to the string. Where could they come from? Thank you in advance.

Code:

var_dump(iconv('windows-1251', 'utf-8', $_POST['data'])); echo '<br>'; var_dump(json_decode($_POST['data'], true)); echo '<br><br>'; $ar = array( 1 => array( 1 => '11', 2 => '222', 4 => '4444', 'sid' => '1', ), ); var_dump($ar = json_encode($ar)); echo '<br>'; var_dump(json_decode($ar, true)); /* Вывод: string(137) "{"1":{"1":"11","2":"222","4":"4444","sid":"1"}}" NULL string(47) "{"1":{"1":"11","2":"222","4":"4444","sid":"1"}}" array(1) { [1]=> array(4) { [1]=> string(2) "11" [2]=> string(3) "222" [4]=> string(4) "4444" ["sid"]=> string(1) "1" } } Тип ошибки: JSON_ERROR_SYNTAX - Syntax error */ 

    2 answers 2

    I laugh at my own inattention, thanks ...
    The correct answer is: I have $_POST processed by the htmlspecialchars () function, so the string came with the replacement of quotes. And this was not noticeable when displayed on the screen.
    In short, $_POST['data'] = htmlspecialchars_decode($_POST['data']); solved the problem. =)

    • Wrong answer. That's right - just do not handle the $ _POST htmlspecialchars function ! - Pavel Mayorov

    I had even more fun - double quotes were escaped from $_POST automatically when writing the WP plugin - I had to pre-process the $_POST variable with the str_replace() function

    • Are they still using magic quotes in 2016 ?! - Pavel Mayorov
    • Surprise from Xampp - Leo240