Question: I have an array:

```
$a = array('foo' => 'fooMe');

```

and I do:

```
print_r($a);

```

which prints:

```
Array ( [foo] => printme )

```

Is there a function, so when doing:

```
needed_function('    Array ( [foo] => printme )');

```

I will get the array `array('foo' => 'fooMe');` back?

Answer: 
There is a nice Online-Tool which does exatly what its name is:

[print\_r to json online converter](https://www.nestforms.com/php-print_r-to-json-online)

From a JSON Object its not far to creating an array with the [json\_decode](http://php.net/json_decode) function:

To get an array from this, set the second paramter to true. If you don't, you will get an object instead.

```
json_decode($jsondata, true);

```
Here is a `print_r` output parser, producing the same expression in PHP syntax. It is written as an interactive Stack Snippet, so you can use it here:

```js
function parse(s) {
    const quote = s => '"' + s.replace(/["\\]/g, '\\$&') + '"';
    const compress = indent => " ".repeat(((indent.length + 4) >> 3) * 4);
    return "$data = " + (s.replace(/\r\n?/g, "\n") + "\n").replace(
        /(Array|\w+ (Object)) *\n *\( *\n|^( *)(?:\[(?:(0|-?[1-9]\d*)|(.*?))\] => |(\) *\n+))|(-?\d+(?:\.\d+)?(?:E[-+]\d+)?)\n|(.*(?:\n(?! *\) *$| *\[.*?\] => ).*)*)\n/gm,
        (_, array, object, indent, index, key, close, number, string) => 
            object ? "(object) [\n"
                   : array ? "[\n"
                           : close ? compress(indent) + "],\n"
                                   : indent ? compress(indent) + (index ?? quote(key)) + " => "
                                            : (number ?? quote(string)) + ",\n"
    ).replace(/,\n$/, ";");
}

// I/O handling
const [input, output] = document.querySelectorAll("textarea");
(input.oninput = () => output.value = parse(input.value))();
```

```css
textarea { width: 23em; height: 12em }
```

```html
<table><tr><th>Input print_r format</th><th>Output PHP syntax</th></tr>
<tr><td><textarea>
Array
(
    [0] => example
    [1] => stdClass Object
        (
            [a[] => 1.43E+19
            ["] => quote
            [] => 
        )
)
</textarea></td><td><textarea readonly></textarea></td></tr></table>
```

### Remarks

* Don't remove any line breaks from the original `print_r` output. For instance, both the opening and closing parentheses after `Array` *must* appear on separate lines.
* Don't change the spacing around `=>` (one space before, one after).
* As `print_r` does not distinguish between `null`, `""` or `false` (it produces no output for these values), nor between `true` and `1` (both are output as `1`), this converter will never produce `null`, `false` or `true`.
* As `print_r` does not distinguish between numbers and strings (`9` could represent a number or a string), this converter will assume that the data type is to be **numeric** when such ambiguity exists.
* `stdClass Object` is supported and translates to `(object) [...]` notation
* `MyClass Object` will be treated as if it was a `stdClass` object.
* String literals in the output are double quoted and literal double quotes and backslashes are escaped.