I’m new to PHP and could not find a proper answer to this.
$whatever = "array('Test','Blah')";
echo $parsed[2]; //This will be "Blah"
I want to create a variable called $parsed which contains $whatever‘s value but as a valid array instead of a string.
I’m aware I can just create the array by removing the quotation marks around it like this:
$whatever = array('Test','Blah');
In the actual code I’m working on, though, this isn’t a possibility. Also, in my actual code, the array is multidimensional, so something involving a character replacement would probably be impractical, however I’m not ruling it out if it’s the best option.
So to sum it up, what’s the best way to go about parsing a string as an array in PHP?
Use the
evalfunction: http://php.net/manual/en/function.eval.php.Be careful to check for the
$whatevervariable contents, because any PHP code can be executed.