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);

```
Quick function (without checks if you're sending good data):

```
function textToArray($str)
{
    $output = [];

    foreach (explode("\n", $str) as $line) {

        if (trim($line) == "Array" or trim($line) == "(" or trim($line) == ")") {
            continue;
        }

        preg_match("/\[(.*)\]\ \=\>\ (.*)$/i", $line, $match);

        $output[$match[1]] = $match[2];
    }

    return $output;
}

```

This is the expected input:

```
Array
(
    [test] => 6
)

```