Search code examples
phparraysjsonpretty-print

print array with proper indentation


foreach ( $custom_fields as $field_key => $field_values ) {
    foreach ( $field_values as $key => $value )
        echo $field_key . '<br>';
        echo '<pre>' , print_r( $value,true) , '</pre>';
}

Although I'm using print_r with <pre> tag, the ouput looks like this:

a:4:{i:0;s:6:"Casual";i:1;s:6:"Serene";i:2;s:6:"Unique";i:3;s:9:"Whimsical";}

shouldn't it be listed vertically with proper indentation? What am I doing wrong?


Solution

  • This is a serialized array, use unserialize() to turn it back to an array again:

    foreach ( $custom_fields as $field_key => $field_values ) {
        foreach ( $field_values as $key => $value ) {
            $value = unserialize($value);
            echo $field_key . '<br/>';
            echo '<pre>' , print_r($value, true) , '</pre>';
        } 
    }
    

    What it would look like:

    http://codepad.viper-7.com/5Rppb3