Question

This is the first time i ever face, that var_dumping json_encode of an array resulting boolean value.

I have an array that was resulted from unserialization. I var_dumped it and made sure that it is a valid array. The result is like below. This is just a piece, not entire debug view.

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 json_encode it with max depth=3. Note, i am using PHP 5.5 and running on Apache 2.4, so that it supports depth parameter on json_encode(). This is what i was doing.

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

Finally i got $array var_dumped properly and $json var_dumped with result of false boolean value. What's wrong with it? Is it due to early PHP release that unproper var_dump would result false boolean? Have anyone ever faced this?

UPDATE:

This is the serialize string. The entire string is too bunch so i paste only a piece.

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 ... 
Was it helpful?

Solution

The depth of $array is definitely greater than 3. Just make depth a larger number.

On a side note, you may also use json_last_error to see the error message in the case that json_encode fails.

OTHER TIPS

Try this function as it's already proposed at 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;   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top