0

找不到完全正确的答案,所以希望有人能提供帮助。基本上想创建一个数组,然后从搜索中返回结果,例如

$tsql = "SELECT date, staffid, ID,status, eventid, auditid from maincalendar";  

$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET ); 
$stmt = sqlsrv_query( $conn, $tsql , $params, $options);      
$calarray=array();
while($row = sqlsrv_fetch_array($stmt)) {
    $rowresult = array();
    $rowresult["status"] = $row['status'];
    $rowresult["eventid"] = $row['eventid'];
    $rowresult["caldate"] = $row['date'];
    $rowresult["staffid"] = $row['staffid'];
    $rowresult["ID"] = $row['ID'];
    $rowresult["auditid"] = $row['auditid'];
    $calarray[] = $rowresult; 
}

然后我想搜索匹配 'caldate' 和 'staffid' 的值并返回 $calarray 中的关联条目

4

4 回答 4

0

尝试这个:

$matches = array();
$caldate = //your desired date;
$staffid = //your desired id;

foreach($calarray as $k => $v){
   if(in_array($caldate, $v['caldate']) && in_array($staffid, $v['staffid'])){
      $matches[] = $calarray[$k];
   }
}

$matches 应该是包含您想要的所有结果的数组。


还:

while($row = sqlsrv_fetch_array($stmt)) {
   $rowresult = array();
   $rowresult["status"] = $row['status'];
   $rowresult["eventid"] = $row['eventid'];
   $rowresult["caldate"] = $row['date'];
   $rowresult["staffid"] = $row['staffid'];
   $rowresult["ID"] = $row['ID'];
   $rowresult["auditid"] = $row['auditid'];
   $calarray[] = $rowresult; 
}

可以简写为:

while($row = sqlsrv_fetch_array($stmt)) {
    $calarray[] = $row;
}
于 2013-03-11T13:26:22.930 回答
0

也许此代码片段可以解决您的问题。我不是 PHP 程序员,所以没有保证。

function searchInArray($array, $keyword) {
    for($i=0;$i<array.length();$i++) {
        if(stristr($array[$i], $keyword) === FALSE) {
            return "Found ".$keyword." in array[".$i."]";
        }
     }
}
于 2013-03-11T13:26:48.227 回答
0

我建议以下,

  • 获取您正在显示的当月所需的所有数据,使用col BETWEEN x AND y
  • 将它们添加到 PHP 中的数组中,使用staffidcaldate作为键

像这样的东西;

$calarray[$row['staffid'] . '-' . $row['date']][] = $row;

不确定单个员工编号/日期组合是否每天可以有一个或多个事件,如果不是,您可以删除[]

要检查我们是否有特定人员/日期组合的信息,请使用isset

if (isset($calarray[$staffid . '-' . $mydate]) { ... }
于 2013-03-11T13:26:52.267 回答
0

为要查询的字段添加索引,然后将搜索移至 sql 查询。您还可以在循环内进行简单的过滤。因此,根据您需要的搜索,您将填充多个数组而不是一个数组。

于 2013-03-11T13:27:58.930 回答