34

这是我第一次面对数组的 var_dumping json_encode 产生的布尔值。

我有一个由反序列化产生的数组。我 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)

......

之后我用最大深度 = 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,并且$jsonvar_dumped 的结果为假布尔值。它出什么问题了?是否由于早期的 PHP 版本,不正确的 var_dump 会导致错误的布尔值?有没有人遇到过这种情况?

更新:

这是序列化字符串。整个字符串太束了,所以我只粘贴了一段。

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 ... 
4

2 回答 2

62

的深度$array肯定大于3。只需将深度设置为更大的数字即可。

附带说明一下,您还可以json_last_error在 json_encode 失败的情况下查看错误消息。

于 2013-10-18T02:26:23.897 回答
0

试试这个功能,因为它已经在 php.net 提出

function array_to_json( $array ){

if( !is_array( $array ) ){
    return false;
}

$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
if( $associative ){

    $construct = array();
    foreach( $array as $key => $value ){

        // We first copy each key/value pair into a staging array,
        // formatting each key and value properly as we go.

        // Format the key:
        if( is_numeric($key) ){
            $key = "key_$key";
        }
        $key = '"'.addslashes($key).'"';

        // Format the value:
        if( is_array( $value )){
            $value = array_to_json( $value );
        } else if( !is_numeric( $value ) || is_string( $value ) ){
            $value = '"'.addslashes($value).'"';
        }

        // Add to staging array:
        $construct[] = "$key: $value";
    }

    // Then we collapse the staging array into the JSON form:
    $result = "{ " . implode( ", ", $construct ) . " }";

} else { // If the array is a vector (not associative):

    $construct = array();
    foreach( $array as $value ){

        // Format the value:
        if( is_array( $value )){
            $value = array_to_json( $value );
        } else if( !is_numeric( $value ) || is_string( $value ) ){
            $value = '"'.addslashes($value).'"';
        }

        // Add to staging array:
        $construct[] = $value;
    }

    // Then we collapse the staging array into the JSON form:
    $result = "[ " . implode( ", ", $construct ) . " ]";
}

return $result;   
}
于 2021-12-15T00:53:17.433 回答