问题描述
我遇到以下错误:
警告:array_map():参数#2 应该是一个数组...
警告:implode():在第 1918 行 wp-includes/class-wp-query.php 中传递的参数无效
Warning : implode(): Invalid arguments passed in wp-includes/class-wp-query.php on line 1918
在我的 php 上,我从另一个页面收到一个 var,我这样做了:
On my php I am receiving a var from another page and I do:
$myId = $_POST['varPostId'];
$parts = explode(',', $myId);
然后我需要读取该值
query_posts(array(
'post__in' => $myId
));
但我收到了上述错误.
如果我这样做:
'post__in' => $parts
));
我得到一个空白页.
我尝试使用 implode
$myId = $_POST['varPostId'];
$parts = implode(',', $myId);
并直接在查询中获取值
query_posts(array(
'post__in' => $_POST['varPostId']
));
$_POST['varPostId']
是一个单一的值,如 405
$_POST['varPostId']
is a single value like 405
推荐答案
您从 POST
请求中获得的值似乎只包含一个应该是 int 的帖子 ID,但是可能是一个字符串.您需要进行一些类型和错误检查.像下面这样的东西应该可以工作.我看到 query_posts
和 post__in代码>,我假设 WordPress 在这里发挥作用?
It seems that the value you're getting from the POST
request is containing just a single post ID which should be an int but is possibly a string. You'll need to do some type and error checking. Something like the following should hopefully work. I see query_posts
and post__in
, I'm assuming WordPress is at play here?
// Put your ID into a variable and sanitize it
$myId = sanitize_key( $_POST['varPostId'] );
// Force it to an int (in case it's not from sanitize_key)
$myId = (int) $myId;
// Don't explode because it's not appropriate here
// $parts = explode( ',', $myId );
// Add the post id to the arguments as an array
query_posts(
array(
'post__in' => array( $myId )
)
);
为了跟进为什么 explode
在这里不合适,它用于转换字符串,通常是这样的:
To follow up on why explode
isn't appropriate here, it's used to convert a string, usually something like this:
哦,你好,世界
放入一个包含多个项目的数组,如下所示:
Into an array with multiple items like the following:
$asdf = array(
0 => 'oh',
1 => 'hello',
2 => 'world'
);
但是您似乎没有逗号分隔的帖子 ID 字符串,您只有一个,所以最好不要在这里使用.
But you it seems that you don't have a comma separated string of post ids, you just have the one so it's best not used here.
如评论中所述,explode
接受一个字符串并将其拆分为一组项目.implode
正好相反.它接受一个数组并将其压缩为一个字符串.
As already stated in the comments, explode
takes a string and splits it into an array of items. implode
does the opposite. It takes an array and condenses it down into a string.
所以你可以有这样的东西:
So you could have something such as:
$myArray = array(
0 => 'hello',
1 => 'world'
);
// Convert to a string
$myString = implode( ', ', $myArray );
echo $myString; // prints: hello, world
// Convert back to an array
$myString = explode( ', ', $myArray );
var_dump( $myString ); // prints the array above
这篇关于收到警告:implode():收到 var 值后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!