2016-09-15 75 views
-2

問題在於我必須輸入搜索短語的全名。 也許我可以在此使用strpos?如何在排序中使用strpos搜索foreach

if ($filter && $searchOption && $searchPhrase && $sortField == "createDate" && $order == "asc") { 
     usort($caseList, function ($a, $b) { 
      /* @var $a CMCase */ 
      /* @var $b CMCase */ 
      $time1 = strtotime($a->createDate); 
      $time2 = strtotime($b->createDate); 
      return $time1 > $time2; 
     }); 

     } else if ($filter && $searchOption == "search_customer" && $searchPhrase && $sortField && $order) { 

     $list = $caseList; 
     $caseList = array(); 
     foreach ($list as $case) { 
      if ($case->customerName == $searchPhrase) { 
       $caseList[] = $case; 
      } 
     } 
    } 
+0

什麼是輸入和期望是什麼?提供一些數據。 –

+0

請進一步解釋你想要做什麼。 – KhorneHoly

+0

請提供您想要實現的示例輸入和輸出。 – Dmytrechko

回答

0

這是我的解決方案:

// Searching 
    { 
     if ($filter && $searchOption == "search_customer" && $searchPhrase && $sortField && $order) { 

      $list = array(); 
      foreach ($caseList as $case) { 
       if (stripos($case->customerName,$searchPhrase)!== FALSE) { 
        $list[] = $case; 
       } 
      } 
      $caseList = $list; 

     } else if ($filter && $searchOption == "search_request" && $searchPhrase && $sortField && $order) { 

      $list = array(); 
      foreach ($caseList as $case) { 
       if (stripos($case->identifier,$searchPhrase)!== FALSE) { 
        $list[] = $case; 
       } 
      } 
      $caseList = $list; 
     } 
    } 
0

你可以重寫你的代碼是這樣的:

$result = array_filter($caseList, function($case) use ($searchPhrase){ 
    return stripos($case->customerName, $searchPhrase) !== FALSE; 
}) 

但你仍然需要更具體。 !我用stripos來區分大小寫的搜索。參見manual

P.S.剛剛嘗試這一點,而不是你的代碼:

if ($searchOption == "search_customer" && $searchPhrase) use ($searchPhrase) { 
    $list = array_filter($caseList,function($case) { 
     return stripos($case->customerName, $searchPhrase) !== false; 
    }); 
} 
+0

謝謝Okey我會嘗試重寫我的代碼,但是我可以在其中插入$ searchPhrase? – Adrian

+0

我編輯了我的答案 – Dmytrechko

+0

看看我編輯我的問題,重寫我的代碼,但結果與以前一樣。 ? – Adrian