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.

3
  • 5
    Store it as a json_encode string? Commented Mar 13, 2015 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. Commented Mar 13, 2015 at 16:21
  • Recommend JSON myself, as it's more portable than using serialize. Commented Mar 13, 2015 at 16:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For some reason, I was resistant to doing this until I realized it would work like a charm. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.