Jus*_*ine 2 php arrays variables wordpress
只是一个快速的新手PHP语法问题.
我有一个变量,让我们调用它$tabs,它总是包含一个用逗号分隔的数字串,如下所示:24,35,43,21
I also have a variable that's an array:
$args = array('post_type' => 'page', 'post__in' => array(27,19,29), 'order' => 'ASC');
Run Code Online (Sandbox Code Playgroud)
That's of course WordPress. What I need to do, is place the content of the $tabs variable (numbers) where the numbers inside the array are. I'm rather new to PHP, only understand how to modify some things, but this I can't figure out, no idea how the syntax for it should go.
Anyone able to help? Thanks.
$args['post__in'] = array_merge($args['post__in'], explode(',', $tabs));
Run Code Online (Sandbox Code Playgroud)
让我们解释一下我做了什么,这样你就可以选择一两件事:
explode(',', $tabs) 在分隔符处将字符串拆分为多个片段并将其放入数组中.array_merge($arr1, $arr2) 将合并两个数组.$args['post__in'] 将访问由键指定的数组元素.请注意,在这种情况下,array_merge只会附加值,您可能会得到重复的数字.要摆脱重复,只需将合并包装进去array_unique.像这样:
$args['post__in'] = array_unique(array_merge($args['post__in'], explode(',', $tabs)));
Run Code Online (Sandbox Code Playgroud)
当然,当你只想用全新的数字替换这些数字时,这个简单的案例就是
$args['post__in'] = explode(`,`, $tabs);
Run Code Online (Sandbox Code Playgroud)