Php 检查并比较数组与查询的post类型
我有一个php比较,这是工作Php 检查并比较数组与查询的post类型,php,arrays,comparison,Php,Arrays,Comparison,我有一个php比较,这是工作 if ( $query->get('post_type') == 'motogp-2014') 但我需要检查多个帖子类型,我想知道是否有一个简单的方法或保持这一切在一行 我试过这个 $motogp_seasons = array('motogp-2013','motogp-2014'); if ( $query->get('post_type') == $motogp_seasons ) 而这个 $motogp_seasons = array('m
if ( $query->get('post_type') == 'motogp-2014')
但我需要检查多个帖子类型,我想知道是否有一个简单的方法或保持这一切在一行
我试过这个
$motogp_seasons = array('motogp-2013','motogp-2014');
if ( $query->get('post_type') == $motogp_seasons )
而这个
$motogp_seasons = array('motogp-2013','motogp-2014');
if ( in_array( $query->get('post_type'), $motogp_seasons ) )
但似乎都不管用 我能帮你找到另一个php解决方案吗
非常感谢您可以使用
foreach
遍历数组,然后像这样进行比较
$motogp_seasons = array('motogp-2013','motogp-2014');
foreach($motogp_seasons as $motogp_seasons_val)
{
if ($query->get('post_type') == $motogp_seasons_val)
{
echo $motogp_seasons_val." is equal <br/>";
}
else
{
echo $motogp_seasons_val." is not equal <br/>";
}
}
$motogp_seasures=array('motogp-2013'、'motogp-2014');
foreach($motogp\u seasures作为$motogp\u seasures\u val)
{
如果($query->get('post_type')==$motogp\u val)
{
echo$motogp_seasures_val.“等于
”;
}
其他的
{
echo$motogp_seasures_val.“不等于
”;
}
}
尝试将变量设置为全局变量
global $motogp_seasons
_数组中的
应该可以工作。我不确定您的get函数返回了什么。@Rikesh ooooops,我忘了将$motogp_seasons global:-/@Joshc您还没有告诉我们您正在使用一个函数it@RameshGaur我知道,哥们,这是一个愚蠢的错误,你没有在你的问题中说你正在使用一个函数
$motogp_seasons = array('motogp-2013','motogp-2014');
function order_events_by_date($query) {
global $motogp_seasons;
if ( in_array( $query->get('post_type'), $motogp_seasons ) ) {
// Stuff here
}
}