2016-05-16 93 views
0

我們有一個PHP腳本,它可以循環訪問來自不同網站的許多XML/CSV文件。現在我們設法建立一個好的XML/CSV解析器腳本。PHP:循環訪問數組/字符串或其他內容的最快方法

我們編寫的PHP腳本循環了一些BIG XML或CSV文件。在這些XML或CVS文件中包含來自不同產品的條形碼。

眼下前的腳本開始我充滿了這樣的產品ID +條碼從MySQL的數組:

function Barcodes_Array() { 

    $sql = "SELECT ProductId, Barcode FROM Products WHERE (Barcode <> '') "; 

    $res = mysql_query($sql); 

    while ($rijen = mysql_fetch_assoc($res)) {  
     $GLOBALS['arrBarcodes'][] = $rijen; 
    } 

    } 

通過XML(或CSV)我們每次循環文件,我們必須檢查條碼存在於數組中並返回產品ID。

對於在功能搜索:

$ =產品編號SearchBarcodeProduct($ EanNr, '條形碼');

然而功能:

function SearchBarcodeProduct($elem, $field) 
{ 
    $top = sizeof($GLOBALS['arrBarcodes']) - 1; 
    $bottom = 0; 

    $ProductId = 0; 

    while($bottom <= $top) 
    { 
     if($GLOBALS['arrBarcodes'][$bottom][$field] == $elem) {   
      return $GLOBALS['arrBarcodes'][$bottom]['ProductId']; 
     } 
     else { 

      if (is_array($GLOBALS['arrBarcodes'][$bottom][$field])) { 
       if (in_multiarray($elem, ($GLOBALS['arrBarcodes'][$bottom][$field]))) { 
        return $GLOBALS['arrBarcodes'][$bottom]['ProductId']; 
       } 
      } 

     } 

     $bottom++; 
    }   

    return $ProductId; 
} 

填寫數組中,因爲它永遠每次把我們要求MySQL的產品表。

我現在的問題是:

它仍然需要很長的時間,每次通過條形碼的陣列循環。對於其他任何解決方案,有沒有更快的方法,也許是一種數組的另一種方式? 有人可以幫助請我像這個愚蠢的東西幾周工作!

+3

其不好的做法使用'$ GLOBALS'這樣 – 2016-05-16 22:34:02

+0

你有沒有考慮建立你要查找多個條形碼的列表,然後在列表中達到一定規模查詢所有的人都在一次?這將避免每次執行新查詢的開銷,但您仍然可以從MySQL索引中受益。 – Chris

+0

您可能會想要查看在最初加載時對查找數組進行排序,然後如何對其執行對數搜索。 (實際上,只需在查詢中添加ORDER BY barcode即可完成排序部分。) – Uueerdo

回答

0

爲什麼你需要2個功能?

嘗試只是一個

function itemBarcode($id) { 
    $id = intval($id); 
    $sql = "SELECT ProductId, Barcode FROM Products WHERE ProductId = $id Barcode <> '') "; 

    $res = mysql_query($sql); 

    if ($row = mysql_fetch_assoc($res)) {  
     return $row['barcode']; 
    } else { 
     return 0; 
    } 
} 

更新,如果你需要通過條碼您可以創建另一個功能來搜索:

function itemProduct($barcode) { 
    $sql = "SELECT ProductId, Barcode FROM Products WHERE Barcode = $barcode "; 
    $res = mysql_query($sql); 

    if ($row = mysql_fetch_assoc($res)) {  
     return $row['ProductId']; 
    } else { 
     return 0; 
    } 
} 
+0

我們不尋找產品ID!我們總是尋找條形碼這是什麼是通用的;) – Faith

+0

檢查更新。我仍然不明白爲什麼你需要2個功能。 – Alex

+0

第一個函數填充數組,以便腳本將在數組中查找,而不是數據庫表的150.000倍。 – Faith

0

聽起來像是你缺少你的Barcode列的索引在您的數據庫中。使用大概獨特的單索引列的單行查找應該快速起泡。

CREATE INDEX Barcode_Index ON Products (Barcode) 

然後簡單:

SELECT ProductId FROM Products WHERE Barcode = *INPUT* 

你也可以將索引UNIQUE如果NULL條碼,他們目前=「」如果有這些不止一個。

另一種選擇是鍵控你有條形碼的陣列:

while ($rijen = mysql_fetch_assoc($res)) {  
    $GLOBALS['arrBarcodes'][$rijen['Barcode']] = $rijen; 
} 

甚至只是:

while ($rijen = mysql_fetch_assoc($res)) {  
    $GLOBALS['arrBarcodes'][$rijen['Barcode']] = $rijen['ProductId']; 
} 

然後,你可以做一個直一看,

$ProductId = isset($GLOBALS['arrBarcodes'][$Barcode]) 
    ?$GLOBALS['arrBarcodes'][$Barcode]['ProductId'] 
    :0; 

或:

$ProductId = isset($GLOBALS['arrBarcodes'][$Barcode]) 
    ?$GLOBALS['arrBarcodes'][$Barcode] 
    :0; 

N.B請閱讀有關使用$GLOBALSmysql_query的評論中的警告。

  • 如果您需要它,請將條形碼數組存儲在對象或變量中。
  • PDO非常方便,我認爲它也可以爲您在返回時鎖定返回的數組。
+0

非常奇怪的嘗試兩種方式,但在MySQL結果表中搜索的行數多於數組搜索: ** while($ rijen = mysql_fetch_assoc($ res)){GLOBALS ['arrBarcodes'] [$ rijen ['Barcode']] = $ rijen ['ProductId']; } ** – Faith

+0

對不起,我不明白你的意思..我認爲條碼應該在數據庫表中是唯一的。問題是什麼?這兩個選項都應該更快,特別是內存中的鍵控陣列。 – Arth

+0

就像我說的。我已經有一個索引條碼欄。當我在產品表中使用搜索條形碼時,我得到了更多結果,然後$ ProductId = isset($ GLOBALS ['arrBarcodes'] [$ Barcode])?$ GLOBALS ['arrBarcodes'] [$ Barcode] ['ProductId 「]:0; ***當在數組中搜索條碼*** – Faith

相關問題