2016-05-16 37 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

相关问题