เจอว่า json_encode มันไม่ return ผลลัพธ์ ออกมาเลยในหน้าเดียวกัน แต่ต่างกันที่ product id ลอง print_r / var_dump ตัว array ที่เข้าไปใน function ก็ปกติดี ไม่ได้มากมาย หรือมีเครื่องหมายพิเศษอะไร ลอง debug ดูถึงเจอว่าถ้าใช้ function json_last_error() มันจะ return เลข 7 กลับมาในหน้าที่มีปัญหา ลองค้นดูรหัส 7 คือ infinity number ลอง unset ตัว key ที่เป็น float(-INF) ดูก็กลับเป็นปกติจริงๆ
รหัสต่างๆ คือ
Code | Constant | Meaning | Availability |
---|---|---|---|
0 |
JSON_ERROR_NONE |
No error has occurred | |
1 |
JSON_ERROR_DEPTH |
The maximum stack depth has been exceeded | |
2 |
JSON_ERROR_STATE_MISMATCH |
Invalid or malformed JSON | |
3 |
JSON_ERROR_CTRL_CHAR |
Control character error, possibly incorrectly encoded | |
4 |
JSON_ERROR_SYNTAX |
Syntax error | |
5 |
JSON_ERROR_UTF8 |
Malformed UTF-8 characters, possibly incorrectly encoded | PHP 5.3.3 |
6 |
JSON_ERROR_RECURSION |
One or more recursive references in the value to be encoded | PHP 5.5.0 |
7 |
JSON_ERROR_INF_OR_NAN |
One or moreNAN or INF values in the value to be encoded |
PHP 5.5.0 |
8 |
JSON_ERROR_UNSUPPORTED_TYPE |
A value of a type that cannot be encoded was given | PHP 5.5.0 |
9 |
JSON_ERROR_INVALID_PROPERTY_NAME |
A property name that cannot be encoded was given | PHP 7.0.0 |
10 |
JSON_ERROR_UTF16 |
Malformed UTF-16 characters, possibly incorrectly encoded | PHP 7.0.0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | <!doctype html> <html> <head> <meta charset= "utf-8" > <link href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= "stylesheet" > <title>PHP: json_encode error</title> <meta content= "Pitt Phunsanit" name= "author" /> </head> <body> <div class = "container" > <?php $datas = [ 'ininfity' => -9e1000, 'title' => 'title' , ]; ?> <div class = "row" ><label class = "col-md-2" for = "" >Datas Arrya:</label><textarea class = "col-md-10" cols= "100" rows= "6" ><?=var_dump( $datas ); ?></textarea></div> <div class = "row" ><label class = "col-md-2" for = "" >Json Datas:</label><textarea class = "col-md-10" cols= "100" rows= "6" ><?=json_encode( $datas ); ?></textarea></div> <div class = "row" > <label class = "col-md-2" for = "" >Json Error:</label> <div class = "col-md-10" ><?=json_last_error(); ?></div> </div> <table class = "table table-striped" > <caption><strong>JSON error codes</strong></caption> <thead> <tr> <th>Code</th> <th>Constant</th> <th>Meaning</th> <th>Availability</th> </tr> </thead> <tbody class = "tbody" > <tr> <td><strong><code><?=JSON_ERROR_NONE; ?></code></strong></td> <td><strong><code>JSON_ERROR_NONE</code></strong></td> <td>No error has occurred</td> <td class = "empty" > </td> </tr> <tr> <td><strong><code><?=JSON_ERROR_DEPTH; ?></code></strong></td> <td><strong><code>JSON_ERROR_DEPTH</code></strong></td> <td>The maximum stack depth has been exceeded</td> <td class = "empty" > </td> </tr> <tr> <td><strong><code><?=JSON_ERROR_STATE_MISMATCH; ?></code></strong></td> <td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td> <td>Invalid or malformed JSON</td> <td class = "empty" > </td> </tr> <tr> <td><strong><code><?=JSON_ERROR_CTRL_CHAR; ?></code></strong></td> <td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td> <td>Control character error, possibly incorrectly encoded</td> <td class = "empty" > </td> </tr> <tr> <td><strong><code><?=JSON_ERROR_SYNTAX; ?></code></strong></td> <td><strong><code>JSON_ERROR_SYNTAX</code></strong></td> <td>Syntax error</td> <td class = "empty" > </td> </tr> <tr> <td><strong><code><?=JSON_ERROR_UTF8; ?></code></strong></td> <td><strong><code>JSON_ERROR_UTF8</code></strong></td> <td>Malformed UTF-8 characters, possibly incorrectly encoded</td> <td>PHP 5.3.3</td> </tr> <tr> <td><strong><code><?=JSON_ERROR_RECURSION; ?></code></strong></td> <td><strong><code>JSON_ERROR_RECURSION</code></strong></td> <td>One or more recursive references in the value to be encoded</td> <td>PHP 5.5.0</td> </tr> <tr> <td><strong><code><?=JSON_ERROR_INF_OR_NAN; ?></code></strong></td> <td><strong><code>JSON_ERROR_INF_OR_NAN</code></strong></td> <td> One or more <a class = "link" href= "http://php.net/manual/de/language.types.float.php#language.types.float.nan" target= "_blank" ><strong><code>NAN</code></strong></a> or <a class = "link" href= "http://php.net/manual/de/function.is-infinite.php" target= "_blank" ><strong><code>INF</code></strong></a> values in the value to be encoded </td> <td>PHP 5.5.0</td> </tr> <tr> <td><strong><code><?=JSON_ERROR_UNSUPPORTED_TYPE; ?></code></strong></td> <td><strong><code>JSON_ERROR_UNSUPPORTED_TYPE</code></strong></td> <td>A value of a type that cannot be encoded was given</td> <td>PHP 5.5.0</td> </tr> <tr> <td><strong><code><?=JSON_ERROR_INVALID_PROPERTY_NAME; ?></code></strong></td> <td><strong><code>JSON_ERROR_INVALID_PROPERTY_NAME</code></strong></td> <td>A property name that cannot be encoded was given</td> <td>PHP 7.0.0</td> </tr> <tr> <td><strong><code><?=JSON_ERROR_UTF16; ?></code></strong></td> <td><strong><code>JSON_ERROR_UTF16</code></strong></td> <td>Malformed UTF-16 characters, possibly incorrectly encoded</td> <td>PHP 7.0.0</td> </tr> </tbody> </table> </div> </body> </html> |