0

I was looking for in Stack some solution to convert a string variable to an Array. The String variable contents this:

$myvar = 'array("a" => array("b1" => 1, "b2" => "something"), "c" => array("d1" => 1))';

I want to convert as:

$myarray = array(
     "a" => array (   
         "b1" => 1, 
         "b2" => "something"
     ),
     "c" => array("d1" => 1)      
);

I was using json_decode after to convert my huge string to json, I used implode too ...

Using eval, I recieve next error:

Parse error: syntax error, unexpected '<' in tabletocreate.php(51) : eval()'d code on line 1

I used print_r($myvar);

The idea it is I have an model inside a file model.php it is only a dictionary, I read this file inside a string and after to convert again an array, I do it because I need to generate a new database from this data, and I have the model for each catalog I need products, offer, ... obiously each model will be different but with the same structure of array, I wrote in the example

SOLUTION

faintsignal resolved the solution in my case: eval("\$myarray = " . $myvar . ';');

Community
  • 1
  • 1
Dave
  • 7,028
  • 11
  • 35
  • 58

2 Answers2

2

Use eval() http://www.php.net/manual/en/function.eval.php

$myarray = eval($myvar)

But don't do this if you're getting the $myvar from outside your script. Also, there are much better methods to serialize and unserialize data in PHP. For instance see http://www.php.net/manual/en/function.serialize.php

If you're reading a .php file directly you have to deal with <?php and ?>. You can pre-parse the file before passing it to eval(). If your file is as simple as:

<?php (php_declarations_and_stuff ?>

You can just remove <?php and ?>, and the eval() the file.

Another option may be just use include against the file. See http://www.php.net/manual/en/function.include.php. In this case include will eval the file in the global scope. Also see that if your included file uses return, you can just retrieve this value directly (See Example #5 in the include reference):

return.php <?php return 'Ok'; ?>

myfile.php $val = include('return.php'); print $val;

Will print "Ok".

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • $myvar = 'array("a")'; print_r(eval($myvar)); tried it in my code and it is not working, give me an error eval()'d code on line 1 – Dave Mar 23 '14 at 11:43
  • Could you post the error? Also, what exactly are you trying to do? Because if you're trying to just define an array is much simpler than that. – Xavier Rubio Jansana Mar 23 '14 at 11:51
1

eval() will accomplish what you need, but as another poster here advided in his answer, you must use this function with great caution.

$myvar = 'array("a" => array("b1" => 1, "b2" => "something"), "c" => array("d1" => 1))';
eval("\$myarray = " . $myvar . ';');
var_dump($myarray);

outputs

array(2) {
  ["a"]=>
  array(2) {
    ["b1"]=>
    int(1)
    ["b2"]=>
    string(9) "something"
  }
  ["c"]=>
  array(1) {
    ["d1"]=>
    int(1)
  }
}

Someone commented that eval is deprecated but I cannot find any info to support this. However, its use is discouraged, so you might consider it as a last resort when existing PHP functionality will not accomplish your task.

faintsignal
  • 1,828
  • 3
  • 22
  • 30
  • It worked in my PHP server. Thanks faintsignal. I lost one day to try to find a solution but It is something simple I dont understand why eval is deprecated, I look info for you commented and I didnt' find too. ANYWAY THANKS – Dave Mar 23 '14 at 12:15
  • I dont have reputation to do this, sorry I would love to do it – Dave Mar 23 '14 at 18:23
  • Aziz what I did is set the answer in my question, is it correct? – Dave Mar 23 '14 at 22:05