Dave
Dave

Reputation: 7359

In PHP, how can I convert an Array (dictionary) inside String variable to Array structure?

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

Upvotes: 0

Views: 2248

Answers (2)

Xavier Rubio Jansana
Xavier Rubio Jansana

Reputation: 6583

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

Upvotes: 2

faintsignal
faintsignal

Reputation: 1836

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.

Upvotes: 1

Related Questions