1

I am having an array formatted string stored in a variable where i need to convert it in to real array

here is my code

$hello ="array( 
    'numberposts' => -1, 
    'post_type' => 'post', 
    'meta_query' => array( 
        'relation' => 'AND',
             array( 
                'key' => 'subject_id', 
                'value' => 'CE6301', 
                'compare' => '=' 
                ),
             array( 
                'key' => 'regulation', 
                'value' => '2013', 
                'compare' => '=' 
                ),
             )
    )";

So here is an array that i need to convert as formated array in php

So when my array is printed it should look something like this

Array ( [numberposts] => -1 [post_type] => post [meta_query] => Array ( [relation] => AND [0] => Array ( [key] => subject_id [value] => CE6301 [compare] => = ) [1] => Array ( [key] => regulation [value] => 2013 [compare] => = ) ) )

What function i have to use in php to convert string array into an normal array

5
  • 3
    eval(). But be sure to read up on that. Commented Aug 30, 2014 at 12:05
  • @Martijn Nice, I wouldn't have thought of that Commented Aug 30, 2014 at 12:06
  • 2
    saving it to JSON before that would be better though. Commented Aug 30, 2014 at 12:07
  • 3
    If you can help it, you may want to step back and choose JSON over an "array formatted string." It seems you are seeking a solution to a problem which you are creating. Commented Aug 30, 2014 at 12:07
  • eval function is not working , even i parsed as json_encode() not working, i encoded as JSON and i used eval, printing same like normal string but not as an array Commented Aug 30, 2014 at 12:24

1 Answer 1

1

I don't know how you got hold of this but you can use eval() on this one. Just modify the string a little bit to create a valid array in the end. Example:

$hello ="array( 
    'numberposts' => -1, 
    'post_type' => 'post', 
    'meta_query' => array( 
        'relation' => 'AND',
             array( 
                'key' => 'subject_id', 
                'value' => 'CE6301', 
                'compare' => '=' 
                ),
             array( 
                'key' => 'regulation', 
                'value' => '2013', 
                'compare' => '=' 
                ),
             )
)";

$hello = '$hello = ' . $hello . ';';
eval($hello);
echo '<pre>';
print_r($hello);
Sign up to request clarification or add additional context in comments.

2 Comments

As like same i have tried the eval function , but not works for me, but i copied your code it works perfect, Anyway Thank you ghost this is what i expected
Again, read some info about eval(), you could potentially create a security problem. Eval should a last-option-solution only.

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.