0

My question is a bit different to most like this, I basically want to do the opposite to this question from Haluk.
So I have a JSON string:

{
    "main":
    {
        "title": "QuickPub",
        "defaultRole": "BU"
    },
    "roles":
    {
        "TU":
        {
            "name": "testUser",
            "code": "TU"
        }
    }
}

and I want to be able to generate a string containing a php array definition from it:

<?php

return [
    "main" =>
    [
        "title" => "QuickPub",
        "defaultRole" => "BU"
    ],
    "roles" =>
    [
        "TU" =>
        [
            "name" => "testUser",
            "code" => "TU"
        ]
    ]
];

?>

EDIT: I have tried json_decode() but this produces a variable, I need a string that I can put in a php file that will reproduce this without using php_decode.

Hazzdood
  • 69
  • 1
  • 10

3 Answers3

1

I think this will solve your problem. First of all convert your json string to an array using json_decode($string,1); then convert that array to string representation using print_r($array,1); this will return your result as array string representation.

For example:

 $json='{}' // its a demo
 $array= json_decode($json,1); // an array
 $result = print_r($array,1);
 echo $result;
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • this would return `( [main] => array ( [title] => QuickPub [defaultRole] => BU ), [roles] => ( [TU] => ( [name] => testUser [code] => TU ) ) )` which is not valid php – Hazzdood Nov 06 '17 at 12:02
0

This is an adaptation of Being Sunny's answer, but using the var_export() function rather than print_r.

As described here by phihad

var_export prints valid php code. Useful if you calculated some values and want the results as a constant in another script

the script:

$json   = '{"main":{"title": "QuickPub","defaultRole": "BU"},"roles":{"TU":{"name": "testUser","code": "TU"}}}';
$array  = json_decode($json, 1);
$result = var_export($array, 1);
echo $result;

produces:

array(
    'main'  => array(
        'title'       => 'QuickPub',
        'defaultRole' => 'BU',
    ),
    'roles' => array(
        'TU' => array(
            'name' => 'testUser',
            'code' => 'TU',
        ),
    ),
)
Hazzdood
  • 69
  • 1
  • 10
-1

This can be achieved using this code:

$output = 'return ' . rtrim(preg_replace(['/{/', '/}/', '/:/'], ['[', ']', ' =>'], $json)) . ';';

this replaces { with [, } with ], and : with =>, trims any whitespace from the end, appends a ; and prepends a return statement.

this produces the output requested in the question bar the open and closing php tags.

Hazzdood
  • 69
  • 1
  • 10
  • There is a built-in to echo PHP variables as PHP-compatible strings. I suggest you look for that, rather than building your own solution. – CJ Dennis Nov 04 '17 at 01:05
  • This is going to fail very hard as soon as any of your property names or values contain any of those characters. – Evert Nov 04 '17 at 01:29
  • I realise that now, but I'll leave it as an option – Hazzdood Nov 06 '17 at 12:04