php - How to expand a variable within an array so the surrounding array is valid -


i'm having problem don't know how describe. need build dynamic array, , include within array. let me expand. here's code:

$myarrayarray = ''; $categoriesterms = $catlist = rtrim($categoriesterms,','); $categoriestocheck = explode(',',$categoriesterms);  foreach($categoriestocheck $key=>$value){     $myarrayarray .= "array(";     $myarrayarray .= "'taxonomy' => 'map_location_categories',";     $myarrayarray .= "'terms' => array(".$value."),";     $myarrayarray .= "'field' => 'slug',";     $myarrayarray .= "'operator' => 'and'";     $myarrayarray .= "),"; }            $myarrayarray .= "array("; $myarrayarray .= "'taxonomy' => 'map_location_categories',"; $myarrayarray .= "'terms' => array('event'),"; $myarrayarray .= "'field' => 'slug',"; $myarrayarray .= "'operator' => 'or'"; $myarrayarray .= "),"; echo $myarrayarray;  $locationargs = array(     'post_type' => 'maplist',     'orderby' => $orderby,     'order' => $orderdir,     'numberposts' => -1,     'tax_query' => array($myarrayarray), );  $maplocations = get_posts($locationargs); 

this not generate error, fails limit data returns in way. if print $myarrayarray variable, search combines paint , pressurized-cylinders:

array(     'taxonomy' => 'map_location_categories',     'terms' => array('paint'),     'field' => 'slug',     'operator' => 'and' ), array(     'taxonomy' => 'map_location_categories',     'terms' => array('pressurized-cylinders'),     'field' => 'slug',     'operator' => 'and' ), array(     'taxonomy' => 'map_location_categories',     'terms' => array('event'),     'field' => 'slug',     'operator' => 'or' ), 

if put in place of variable in code, works great. so, variable isn't malformed, it's not resolving within other array. maybe i'm idiot , isn't possible? doing wrong?!?!?! driving me insane , don't know how phrase search solution.

you're generating string, not array.

$str = 'hello,there'; $arr = array($str); 

does not produce

   $arr = array(        'hello', // element #1        'there'  // element #2    ); 

it produces

$arr = array(     'hello,there' // single element #1 ); 

if want produce nested arrays, skip whole strings business

$data = array(); foreach($categoriestocheck $key=>$value){     $data[] = array(        'taxonomy' => 'map_location_categories',        'terms' => array($value.),        etc..     ); } $locationargs = array(     ...     data => $data ); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -