我有以下代码:
$group = [];
$comparison = '=';
$group = array('$and'=>array());
$filters = ['abc', 'xyz'];
foreach ($filters as $filter) {
$group['$and'][] = array(
'team_id' => array (
($comparison == '=' ? '$nin' : '$in') => array($filter)
)
);
}
echo json_encode($group, true);
我尝试了一些诸如$ group ['$ and'] [] ['team_id'] [$ comparison] = $ filter之类的事情;在foreach循环中没有运气
这会产生以下结果:
{
"$and":[
{
"team_id":{
"$nin":[
"abc"
]
}
},
{
"team_id":{
"$nin":[
"xyz"
]
}
}
]
}
我正在尝试获取:
"$and":[
{
"team_id":{
"$nin":[
"abc",
"xyz"
]
}
}
]