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

10
  • Yes, json_decode is the way, but you don't say what happened when you tried it. Commented Mar 23, 2014 at 11:32
  • If you are in control over how the array is saved / persisted, you should have a look at the serialize() and unserialize() functions. Commented Mar 23, 2014 at 11:32
  • 1
    Your first example is not a valid PHP string Commented Mar 23, 2014 at 11:33
  • sorry true Utkanos, it returns me Array only with one element whcih is the string Commented Mar 23, 2014 at 11:33
  • 1
    @faintsignal My bad, it isn't deprecated. In javascript it is(but this is PHP). But, be careful by using it with external resources. Commented Mar 23, 2014 at 12:43

2 Answers 2

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".

Sign up to request clarification or add additional context in comments.

2 Comments

$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
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.
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.

3 Comments

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
I dont have reputation to do this, sorry I would love to do it
Aziz what I did is set the answer in my question, is it correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.