Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String "array" to real array

Tags:

arrays

php

Now I got the string of an array, like this :

$str = "array('a'=>1, 'b'=>2)";

How can I convert this string into real array ? Is there any "smart way" to do that, other that use explode() ? Because the "string" array could be very complicated some time.

Thanks !

like image 493
JuLy Avatar asked Nov 24 '10 11:11

JuLy


1 Answers

If you can trust the string, use eval. I don't remember the exact syntax, but this should work.

$arr = eval($array_string);

If the string is given by user input or from another untrusted source, you should avoid eval() under all circumstances!

To store Arrays in strings, you should possibly take a look at serialize and unserialize.

like image 83
joni Avatar answered Oct 05 '22 20:10

joni