2

I have a string that contains this:

array(a=1, b=2, c=array(ca=23, cb=45),d=array(da=342, db=array(dba=3154, dbb=8746), dc=765),e=8)

Now, I need to create an array from the contents of this string. I tried it this way:

$arrValues = array();
$arrValues = $strVar;
var_dump($arrValues);

But of course, as was to be expected, this just gives you an array with 1 item of type string, and the whole shebang in it.

Any tips?

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Borniet
  • 3,544
  • 4
  • 24
  • 33
  • What is `$strVar`? That initial literal? – Grant Thomas Apr 11 '13 at 10:22
  • @GrantThomas From the OP, I'd assume the string `array(a=1, b=2, c=array(ca=23, cb=45),d=array(da=342, db=array(dba=3154, dbb=8746), dc=765),e=8)`. – h2ooooooo Apr 11 '13 at 10:23
  • Yes indeed, $strVar is the variable that contains the "arraystring" – Borniet Apr 11 '13 at 10:25
  • How "safe" would this string be? If it's quite safe, you can `eval()` it after some modification. – Passerby Apr 11 '13 at 10:25
  • @Borniet Where do you have this string from? Is it something you're putting into a database/file yourself, and could you essentially use [`serialize`](http://www.php.net/manual/en/function.serialize.php) or [`json_encode`](http://php.net/manual/en/function.json-encode.php) to save the data instead, and then use [`unserialize`](http://www.php.net/manual/en/function.unserialize.php) and [`json_decode`](http://www.php.net/manual/en/function.json-decode.php) to get the array back? – h2ooooooo Apr 11 '13 at 10:26
  • Well, it is for an internal tool, and I would be the one to put the string in there, so it is "safe" :-) – Borniet Apr 11 '13 at 10:26
  • @h2ooooooo I input this string myself. It is the return I get from a function, and I copy/paste it back. I have no control over the output of this function however. – Borniet Apr 11 '13 at 10:27

2 Answers2

0

you can use eval() but it is not recommanded unless you are sure of what will contain the string (it may cause security issues)

Edit :

It seems that your array is not in the syntaxe of a php array, you will need to reformat it.

Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
  • 4
    Doesn't look like valid array syntax to be eval'd imho. – Fabrício Matté Apr 11 '13 at 10:24
  • I'd say to `str_replace` the `=` with `=>` but eval + arbitrary replacing without even checking if the `=` is part of a string is a little overkill. Oh it also lacks keys' quoting, never mind. – Fabrício Matté Apr 11 '13 at 10:26
  • even replacing `=` with `=>` and string keys with `"string_key"` doesn't successfully `eval()` this array. Thoughts? – Ejaz Apr 11 '13 at 10:29
0

Before I say anything about using eval(), read this stackoverflow post.

As Yazmat mentioned, you could use eval() if you're very sure that nobody could inject code into this, and use a regex of ([a-zA-Z0-9]+)\s*=\s* replaced with "$1" => to change your code to be PHP array compliant:

<?php
    $strVar = 'array(a=1, b=2, c=array(ca=23, cb=45),d=array(da=342, db=array(dba=3154, dbb=8746), dc=765),e=8)';
    $arrValues = array();

    $phpCompliantStrVar = preg_replace('/([a-zA-Z0-9]+)\s*=\s*/', '"$1" => ', $strVar) . ';';
    //string(157) "array("a" => 1, "b" => 2, "c" => array("ca" => 23, "cb" => 45),"d" => array("da" => 342, "db" => array("dba" => 3154, "dbb" => 8746), "dc" => 765),"e" => 8);"

    //Beware of the evilness that eval can cause! It will suck out your soul if overdone.
    eval('$arrValues = ' . $phpCompliantStrVar);

    print_r($arrValues);
    /*
        Array
        (
            [a] => 1
            [b] => 2
            [c] => Array
                (
                    [ca] => 23
                    [cb] => 45
                )

            [d] => Array
                (
                    [da] => 342
                    [db] => Array
                        (
                            [dba] => 3154
                            [dbb] => 8746
                        )

                    [dc] => 765
                )

            [e] => 8
        )
    */
?>

DEMO

Current limitations:

  • It will ONLY work if your values in the array are all numbers - else you need to change the regex.
Community
  • 1
  • 1
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • This one does the trick! As said above, the tool will be used internal, probably even only by me. So the input is safe. – Borniet Apr 11 '13 at 11:01