How to format var_export to php5.4 array syntax
Asked Answered
W

5

31

There are lots of questions and answers around the subject of valid php syntax from var outputs, what I am looking for is a quick and clean way of getting the output of var_export to use valid php5.4 array syntax.

Given

$arr = [
    'key' => 'value',
    'mushroom' => [
        'badger' => 1
    ]
];


var_export($arr);

outputs

array (
  'key' => 'value',
  'mushroom' => 
  array (
    'badger' => 1,
  ),
)

Is there any quick and easy way to have it output the array as defined, using square bracket syntax?

[
    'key' => 'value',
    'mushroom' => [
        'badger' => 1
    ]
]

Is the general consensus to use regex parsing? If so, has anyone come across a decent regular expression? The value level contents of the arrays I will use will all be scalar and array, no objects or classes.

Wichman answered 19/6, 2014 at 21:15 Comment(10)
you really cant go back to the source and not use var_export ?Chary
array(...) is still a valid syntax for declaring arrays in PHP. Square brackets are nothing but a syntactic sugar.Imputation
Oh, I understand that, yes. I'm using it for configuration files, and it would be nice to be able to return back to the original declaration syntax.Wichman
What about just looping through the array and printing it out however you please?Collocutor
You know the old chinese saying, "If you don't like the default var_export syntax, write your own".Martinelli
Well, thats kind of what I'm asking for... I wouldn't really know where to start there ;)Wichman
I have many flavours of configuration, php arrays, json, xml, ini. I'm writing readers and writers for all of them.Wichman
But do you actually need to write this non-standard format? Why support a non-standard configuration format that you have to, in essence, write your own parser for?Olwen
I don't need to, just trying to leverage less code each time it is parsed. They will initially be provided that way, so for them to change when any new configuration is written back out by the app would be weird in my view.Wichman
I guess that is understandable, but is it really that much of a stretch for a user to work in JSON as opposed to that config? In essence, all you would do is change [] to {}, => to :, and ' to ".Olwen
M
37

I had something similar laying around.

function var_export54($var, $indent="") {
    switch (gettype($var)) {
        case "string":
            return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
        case "array":
            $indexed = array_keys($var) === range(0, count($var) - 1);
            $r = [];
            foreach ($var as $key => $value) {
                $r[] = "$indent    "
                     . ($indexed ? "" : var_export54($key) . " => ")
                     . var_export54($value, "$indent    ");
            }
            return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
        case "boolean":
            return $var ? "TRUE" : "FALSE";
        default:
            return var_export($var, TRUE);
    }
}

It's not overly pretty, but maybe sufficient for your case.

Any but the specified types are handled by the regular var_export. Thus for single-quoted strings, just comment out the string case.

Martinelli answered 19/6, 2014 at 21:41 Comment(5)
I like this. I wonder if there is any way to get it to just output like var_export, and not just to string. Not a dealbreaker, as this is what I was looking for, but it would be interesting to find out?Wichman
Just echo the result yourself.Martinelli
I was just about to comment that same thing, as I realised after I posted. Thanks very much!Wichman
This just garbled my data and put lots of 1's down the leftmost columnStuffy
add case 'integer': case 'double': return $var; for prevent incorrect value via var_export($var, TRUE);Prevalent
C
21

For anyone looking for a more modern-day solution, use the Symfony var-exporter, also available as a standalone library on composer, but included in Symfony by default.

composer require symfony/var-exporter
use Symfony\Component\VarExporter\VarExporter;

// ...

echo VarExporter::export($arr)
Confess answered 10/9, 2019 at 13:31 Comment(1)
Perfect answer!Darwinism
J
12

I realize this question is ancient; but search leads me here. I didn't care for full iterations or using json_decode, so here's a preg_replace-based var_export twister that gets the job done.

function var_export_short($data, $return=true)
{
    $dump = var_export($data, true);

    $dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
    $dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
    $dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties

    if (gettype($data) == 'object') { // Deal with object states
        $dump = str_replace('__set_state(array(', '__set_state([', $dump);
        $dump = preg_replace('#\)\)$#', "])", $dump);
    } else { 
        $dump = preg_replace('#\)$#', "]", $dump);
    }

    if ($return===true) {
        return $dump;
    } else {
        echo $dump;
    }
}

I've tested it on several arrays and objects. Not exhaustively by any measure, but it seems to be working fine. I've made the output "tight" by also compacting extra line-breaks and empty arrays. If you run into any inadvertent data corruption using this, please let me know. I haven't benchmarked this against the above solutions yet, but I suspect it'll be a good deal faster. Enjoy reading your arrays!

Jennie answered 4/2, 2016 at 17:5 Comment(2)
On a note of disclaimer... If there's variance between the formatting of var_export output on different PHP versions or platforms, this may obviously do some funnies. I was unable to find any information on that, so I presume there's none. Tested OK on PHP 5.6.2 and 7.0.2 on W7x64.Jennie
Does the job well with nested arrays, thanks for sharing.Rager
D
12

With https://github.com/zendframework/zend-code :

<?php
use Zend\Code\Generator\ValueGenerator;
$generator = new ValueGenerator($myArray, ValueGenerator::TYPE_ARRAY_SHORT);
$generator->setIndentation('  '); // 2 spaces
echo $generator->generate();
Deandra answered 27/7, 2017 at 6:30 Comment(0)
A
2

As the comments have pointed out, this is just an additional syntax. To get the var_export back to the bracket style str_replace works well if there are no ) in the key or value. It is still simple though using JSON as an intermediate:

$output = json_decode(str_replace(array('(',')'), array('&#40','&#41'), json_encode($arr)), true);
$output = var_export($output, true);
$output = str_replace(array('array (',')','&#40','&#41'), array('[',']','(',')'), $output);

I used the HTML entities for ( and ). You can use the escape sequence or whatever.

Aldine answered 19/6, 2014 at 21:23 Comment(3)
That seems a shaky workaround, as it would displace plain ) parens within string context as well.Martinelli
I like the simplicity of this, and will probably use this to start, but I already know that, as the law of sod states, someone will add ) into their strings.Wichman
Agreed. Edited with an easy solution.Aldine

© 2022 - 2024 — McMap. All rights reserved.