PHP: How to put a variable in an array? PHP: How to put a variable in an array? wordpress wordpress

PHP: How to put a variable in an array?


$args['post__in'] = array_merge($args['post__in'], explode(',', $tabs));

Let's explain what I did so you may pick up a thing or two:

  1. explode(',', $tabs) splits a string into pieces at a separator and puts that pieces in an array.
  2. array_merge($arr1, $arr2) will merge the two arrays.
  3. $args['post__in'] will access an array element specified by a key.

Note that array_merge, in this case, will just append the values and you may end up with duplicate numbers. To get rid of duplicates just wrap the merge in array_unique. Like so:

$args['post__in'] = array_unique(array_merge($args['post__in'], explode(',', $tabs)));

And of course, the trivial case when you just want to replace those numbers with brand new ones is

$args['post__in'] = explode(`,`, $tabs);


$args['post__in'] = explode(",",$tabs);