动态写入变量时在代码中发出问题
Issue in the code while writting variables dynamically
我写了下面几行代码
$advsr_firstname="David"
$advsr_middlename ="";
// note that these two are passed through function parametre
$first_name=array('$or' => array(array("student.first_name" => new MongoRegex("/$advsr_firstname/i"))));
$last_name= array('$or' => array(array("student.last_name" => new MongoRegex("/$advsr_middlename/i"))));
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
$empty = "X9w+";
foreach ($keyarguments as $key => $value)
{
if(${$value} =='' || ${$value} =='0' || ${$value} =="Select")
{
${$key} = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
}
}
$orrollno= array('$or' => array(array("student.roll_no" => new MongoRegex("/$arg/i"))));
$query = array( '$or' => array($first_name,$last_name,$orrollno));
我正在尝试动态编写查询,以便 $query
能够正常工作。
我希望如果“$advsr_middlename”为空,则代码应动态修改循环中的表达式 $last_name,如
$last_name = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
它正在抛出错误消息未定义的变量first_name=>advsr_firstname
....
请帮助我,因为代码不起作用!!!
我认为这可能是解决方案:从
修复 $keyarguments
数组的语法
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
到
$keyarguments = [
'first_name' => 'advsr_firstname',
'last_name' => 'advsr_middlename',
];
我写了下面几行代码
$advsr_firstname="David"
$advsr_middlename ="";
// note that these two are passed through function parametre
$first_name=array('$or' => array(array("student.first_name" => new MongoRegex("/$advsr_firstname/i"))));
$last_name= array('$or' => array(array("student.last_name" => new MongoRegex("/$advsr_middlename/i"))));
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
$empty = "X9w+";
foreach ($keyarguments as $key => $value)
{
if(${$value} =='' || ${$value} =='0' || ${$value} =="Select")
{
${$key} = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
}
}
$orrollno= array('$or' => array(array("student.roll_no" => new MongoRegex("/$arg/i"))));
$query = array( '$or' => array($first_name,$last_name,$orrollno));
我正在尝试动态编写查询,以便 $query
能够正常工作。
我希望如果“$advsr_middlename”为空,则代码应动态修改循环中的表达式 $last_name,如
$last_name = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
它正在抛出错误消息未定义的变量first_name=>advsr_firstname ....
请帮助我,因为代码不起作用!!!
我认为这可能是解决方案:从
修复$keyarguments
数组的语法
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
到
$keyarguments = [
'first_name' => 'advsr_firstname',
'last_name' => 'advsr_middlename',
];