0

I am storing an array in string form in a database for later retrieval: The value of the array happens to be parameters for a filter_val call.

$str = 'array("options" => array("min_range" => 4))';

I know I can use

eval('$options = ' . $str . ';');

to prepare this value for passing to filter_val, but is there any other way to do this?

This related post (while excellent) didn't handle my exact issue.

Community
  • 1
  • 1
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • 5
    Store it as a `json_encode` string? – ʰᵈˑ Mar 13 '15 at 16:18
  • Yeah, if you're going to take it outside the realm of your code then just store it in a way that keeps the information of the array but without the concept of the data structure, which you can easily recreate. I would vote for JSON as well. – yoshiwaan Mar 13 '15 at 16:21
  • Recommend JSON myself, as it's more portable than using `serialize`. – ItalyPaleAle Mar 13 '15 at 16:34

1 Answers1

3

I would not use eval() to get your string functional. For example, the function eval may be disallowed on some hosts, thus your application will not work.

A more appropriate way would be to store your options in a json_encode()'d string, and decode when you need.

$options = json_decode($options_from_db);

Or as John Conde mentions, you can serialise it.

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49