2013-10-18 239 views
19

這是我第一次面對,那var_dumping數組的json_encode生成布爾值。json_encode()返回false

我有一個數組是由unserialization產生的。我var_dumped它,並確保它是一個有效的數組。結果如下所示。這只是一個部分,而不是整個調試視圖。

array (size=3) 
'id' => string 'asco-power-technologies-l-p' (length=27) 
    'name' => string 'asco-power-technologies-l-p' (length=27) 
    'children' => 
    array (size=2) 
     0 => 
     array (size=4) 
      'id' => string 'apl-logistics' (length=13) 
      'name' => string 'APL LOGISTICS' (length=13) 
      'data' => 
      array (size=2) 
       'band' => string 'ASCO POWER TECHNOLOGIES ,L.P.' (length=29) 
       'relation' => string 'Buyer of band' (length=13) 
      'children' => 
      array (size=11) 
       0 => & 
       array (size=3) 
        'id' => string 'trisa-ag' (length=8) 
        'name' => string 'TRISA AG' (length=8) 
        'data' => 
        array (size=2) 
         'band' => string 'APL LOGISTICS' (length=13) 
         'relation' => string 'Buyer of band' (length=13) 
       1 => & 
       array (size=3) 
        'id' => string 'colgate-sanxiao-co-ltd' (length=22) 
        'name' => string 'COLGATE SANXIAO CO LTD' (length=22) 
        'data' => 
        array (size=2) 
         'band' => string 'APL LOGISTICS' (length=13) 
         'relation' => string 'Buyer of band' (length=13) 

.....

Aterwards i相最大深度= 3 json_encode它。請注意,我使用PHP 5.5並在Apache 2.4上運行,因此它支持json_encode()上的深度參數。這就是我正在做的。

$array = unserialize($serial); 
    var_dump($array); 
    $json = json_encode($array, JSON_PRETTY_PRINT, 3); 
    var_dump($json); 

最後我得到$array var_dumped正確和$json用假布爾值結果var_dumped。它出什麼問題了?是否由於早期的PHP版本,不正確的var_dump會導致false布爾值?有沒有人遇到過這個?

UPDATE:

這是連載的字符串。整個字符串太多,所以我只粘貼一塊。

a:3:{s:2:"id";s:27:"asco-power-technologies-l-p";s:4:"name";s:27:"asco-power-technologies-l-p";s:8:"children";a:2:{i:0;a:4:{s:2:"id";s:13:"apl-logistics";s:4:"name";s:13:"APL LOGISTICS";s:4:"data";a:2:{s:4:"band";s:29:"ASCO POWER TECHNOLOGIES ,L.P.";s:8:"relation";s:13:"Buyer of band";}s:8:"children";a:11:{i:0;a:3:{s:2:"id";s:8:"trisa-ag";s:4:"name";s:8:"TRISA AG";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:1;a:3:{s:2:"id";s:22:"colgate-sanxiao-co-ltd";s:4:"name";s:22:"COLGATE SANXIAO CO LTD";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:2;a:3:{s:2:"id";s:25:"apll-on-behalf-oftongfang";s:4:"name";s:25:"APLL ON BEHALF OFTONGFANG";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:3;a:3:{s:2:"id";s:26:"apll-on-behalf-of-zhejiang";s:4:"name";s:26:"APLL ON BEHALF OF ZHEJIANG";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:4;a:3:{s:2:"id";s:33:"apll-on-behalf-of-tongfang-global";s:4:"name";s:33:"APLL ON BEHALF OF TONGFANG GLOBAL";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:5;a:3:{s:2:"id";s:34:"apll-on-behalf-of-ningbo-xiangyuan";s:4:"name";s:34:"APLL ON BEHALF OF NINGBO XIANGYUAN";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:6;a:3:{s:2:"id";s:33:"apll-on-behalf-of-ningbo-homestar";s:4:"name";s:33:"APLL ON BEHALF OF NINGBO HOMESTAR";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:7;a:3:{s:2:"id";s:17:"apll-on-behalf-of";s:4:"name";s:17:"APLL ON BEHALF OF";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:8;a:3:{s:2:"id";s:21:"apl-logistics-sdn-bhd";s:4:"name";s:21:"APL LOGISTICS SDN BHD";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}}i:9;a:4:{s:2:"id";s:21:"apl-logistics-limited";s:4:"name";s:21:"APL LOGISTICS LIMITED";s:4:"data";a:2:{s:4:"band";s:13:"APL LOGISTICS";s:8:"relation";s:13:"Buyer of band";}s:8:"children";a:27:{i:0;a:3:{s:2:"id";s:26:"tri-phil-international-inc";s:4:"name";s:27:"TRI-PHIL INTERNATIONAL INC.";s:4:"data";a:2:{s:4:"band";s:21:"APL LOGISTICS LIMITED";s:8:"relation";s:13:"Buyer of band";}}i:1;a:3:{s:2:"id";s:16:"pt-apl-logistics";s:4:"name";s:17:"PT. APL LOGISTICS";s:4:"data";a:2:{s:4:"band";s:21:"APL LOGISTICS LIMITED";s:8:"relation";s:13:"Buyer of band";}}i:2;a:3:{s:2:"id";s:25:"keygold-manufacturing-inc";s:4:"name";s:26:"KEYGOLD ... 
+0

謝謝!你的問題幫助了我!在我的情況下,json_encode由於一個變量的編碼而返回false ...我需要在執行json_encode之前對變量執行utf8_encode ...現在它起作用了!問候 –

回答

29

$array深度肯定是大於3只是要深度更大的數字。

在附註上,您也可以使用json_last_error在json_encode失敗的情況下查看錯誤消息。

+0

我只是將深度改爲4,5,10甚至100,並且仍然相同。 –

+3

json_last_error顯示未知錯誤。 –

+0

如果您使用PHP頁面中的示例#1,那麼很可能是JSON_ERROR_RECURSION,JSON_ERROR_INF_OR_NAN或JSON_ERROR_UNSUPPORTED_TYPE,因爲如果您沒有修改代碼來處理它們,那麼它們將顯示爲「未知錯誤」。 –