Wasim A.
Wasim A.

Reputation: 9890

Coverting String to PHP Associative Array

How can i convert this string

$str = "array('3'=>'wwm','1'=>'wom')";

to real php associative array...

Upvotes: 0

Views: 470

Answers (3)

Skatox
Skatox

Reputation: 4284

It's simple but REALLY INSECURE

$str = "array('3'=>'www.tension.com','1'=>'www.seomeuo.com','requestedBy'=>'1')";
eval("\$array = $str;");

You never should use this approach, there another ways to do it like: serialize() and unserialize()

Upvotes: 5

hek2mgl
hek2mgl

Reputation: 158090

You can use the eval() function for that:

$str = "array('3'=>'wwm','1'=>'wom')";

eval("\$a=$str;");

var_dump($a);

However using eval() in your code is considered to be risky and you should not use it. Try to use serialize(), unserialize() instead.

Upvotes: 3

nvanesch
nvanesch

Reputation: 2600

First of all. Do not use eval. It is Evil! http://af-design.com/blog/2010/10/20/phps-eval-is-evil/

Secondly. The simple solution would not to be using this string but simply to use "serialize" when you put it in the DB and unserialize when you pull it out. You are storing a very unusual format.

Upvotes: 1

Related Questions