Reputation: 3941
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.
Upvotes: 0
Views: 502
Reputation: 5520
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
Upvotes: 3
Reputation: 10467
Use JSON:
// $json contains {'orderby':'title','order':'ASC'}
$json = json_encode(array('orderby' => 'title', 'order' => 'ASC'));
Upvotes: 0