1

I have something like this:

array('fields' => array(
             0 => array('name'  => 'family_name', 
                        'label' => 'Family Names'),
                  array('name'  => given_names', 
                        'label' => 'Given Names'),
                  array('name'  => 'additional_names',
                        'label' => 'Additional Names'),
                  array('name'  => 'honorific_prefixes', 
                        'label' => 'Honorific Prefixes'),
                  array('name'  => honorific_suffixes', 
                        'label' => 'Honorific Suffixes')
                       )
                    )

in a variable as string. The whole thing is in one database field. If I output the variable, it is a string. I would have an array with the content as subarrays. How do I convert this value into an array?

I searched with google, but I found explode and split and so on, but I think I miss the key word to find any solution.

Thank you for any help in this case.

4
  • The simplest way to be use eval for this, however you must be sure there is no risk of evil injection Commented Aug 19, 2015 at 12:28
  • possible duplicate of how to read output of var_export into a variable in PHP? Commented Aug 19, 2015 at 12:31
  • 1
    Why do you store the data in such a way in your database? you could use JSON, e.g., for easier conversions. Commented Aug 19, 2015 at 12:31
  • 1
    Store result of json_encode. Commented Aug 19, 2015 at 12:32

2 Answers 2

1

Try using eval(), https://secure.php.net/manual/en/function.eval.php

eval("\$array = $string;");
print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

1

Your string is not built correctly to put it into the eval function. Two strings inside don't have the preceding quote and that will lead to a parse error. But you can correct it with:

$string = str_replace("=> given_names'", "=> 'given_names'", $string);
$string = str_replace("=> honorific_suffixes'", "=> 'honorific_suffixes'", $string);

After that you can use the answer of shapeshifter (please mark his answer as the correct one):

eval("\$array = $string;");
var_dump($array);

If you just look for a method to save and restore your arrays you could also use serialize / unserialize.

Comments

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.