0

Is it possible to store soemthing like this in a php string:

$variable = "'orderby'=>'title','order'=>ASC'"

I want to use it in a wordpress loop, but am unsure if I can store multiple arguments in a single variable as a string. For example, I do not want the variable to store multiple arguments, but rather the entire quoted text as a string.

2
  • WordPress itself stores such data as JSON so apart from the fact that Trey's answer is good, you shouldn't use serialize() if you're going to store that data in database. Commented Jun 23, 2012 at 15:30
  • I see...I am trying to solve the problem here: stackoverflow.com/questions/11170480/… Commented Jun 23, 2012 at 15:33

2 Answers 2

3

Your best bet would be to create an array or object and serialize it...:

$variable = array('orderby'=>'title','order'=>'ASC');
$string=serialize($variable);

in response to your question about using it in the loop...

$args=array( 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1);
$more=unserialize($variable);

$loop = new WP_Query( $args+$more );

Json is a faster conversion format as others are trying to point out, and if you're only using this for simple arrays it may be the better solution. Serialize offers some very interesting features for Objects

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

3 Comments

how is the variable $string defined?
it's a mysql safe string that defines an array... you can use unserialize($string) to return it to an array
if I store them as an array, could I use the variable in the loop like so: <?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1, serialize($variable); ) ); ?>
0

Use JSON:

// $json contains {'orderby':'title','order':'ASC'}
$json = json_encode(array('orderby' => 'title', 'order' => 'ASC'));

2 Comments

how would I use JSON in the context of this problem: stackoverflow.com/questions/11170480/…
I've answered that question, please look at it.

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.