2014-01-13 91 views
0

我在這段代碼的末尾想要的是一個數組,它不斷添加給一個數組中的所有$ id。添加到循環結尾的數組

當前代碼在數組中執行15,然後被接下來的15個項目覆蓋。我想能夠在數組中有30個項目的代碼末尾有一個數組。

我下面的代碼:

$idArray = array(); 

do { 
$html = file_get_html($url); 
parseItems($html, $dbh); 
sleep_flush($chunks=1); // ADJUST LATER 
} 

while (!empty($html->find('span[class=load-more-message]', 0))); 

$html->clear(); 
unset($html); 

// ------------------------------------------------- 

function parseItems($html, $dbh) { 

foreach($html->find('div.product-stamp-inner') as $content) { 

$detail['itemid'] = filter_var($content->find('a.product-title-link', 0)->href, FILTER_SANITIZE_NUMBER_FLOAT); 
$id = $detail['itemid']; 
$idArray[] = $id; //Counting and adding items to an array 

$detail['title'] = $content->find('span.title', 0)->plaintext; 
$description = $detail['title']; 

if (!tableExists($dbh, $id, $detail)) { 
    echo $id . " > " . $description . "> Table does not exist >"; 
    createTable($dbh, $id, $description); 
    insertData($dbh, $id, $detail); 
    echo "<br>"; 
} else { 
    echo $id . " > " . $description . "> Table already exists >"; 
    checkData($dbh, $id, $detail); 
    echo "<br>"; 
     } 
    } 
    print_r($idArray); 
} 
+0

從函數定義中拉出'$ idArray'使其成爲全局/會話v良莠不齊。 –

回答

1

這是因爲你重新定義你的$ idArray這裏:

$idArray = array(); 

你可將該類的您$ idArray全球/成員變量..或您可以通過參考傳遞參數:

$idArray = array(); 

do { 
$html = file_get_html($url); 
parseItems($html, $dbh, $idArray); 
sleep_flush($chunks=1); // ADJUST LATER 
} 

while (!empty($html->find('span[class=load-more-message]', 0))); 

$html->clear(); 
unset($html); 

// ------------------------------------------------- 

function parseItems($html, $dbh, &$idArray) { 

foreach($html->find('div.product-stamp-inner') as $content) { 

$detail['itemid'] = filter_var($content->find('a.product-title-link', 0)->href, FILTER_SANITIZE_NUMBER_FLOAT); 
$id = $detail['itemid']; 
$idArray[] = $id; //Counting and adding items to an array 

$detail['title'] = $content->find('span.title', 0)->plaintext; 
$description = $detail['title']; 

if (!tableExists($dbh, $id, $detail)) { 
    echo $id . " > " . $description . "> Table does not exist >"; 
    createTable($dbh, $id, $description); 
    insertData($dbh, $id, $detail); 
    echo "<br>"; 
} else { 
    echo $id . " > " . $description . "> Table already exists >"; 
    checkData($dbh, $id, $detail); 
    echo "<br>"; 
     } 
    } 
    print_r($idArray); 
} 
+0

我已經試過你的建議仍然得到數組:Array([0] => 363741 [1] => 367509 [2] => 915615 [3] => 744035 [4] => 910384 [5] => 771699 [6] => 742662 [7] => 338142 [8] => 139431 [9] => 350215 [10] => 757700 [11] => 377010 [12] => 363763 [13] => ] => 903301)和陣列([0] => 771733 [1] => 771713 [2] => 710078 [3] => 710056 [4] => 710105 [5] => 773022 [6] = > 271594 [7] => 323003 [8] => 917420 [9] => 910406 [10] => 65580 [11] => 19474 [12] => 908886 [13] => 64737 [14] => )'在下一個循環之後。仍然沒有將它們加在一起 – DrDog

+1

我推薦第一個例子在這裏:http://www.php.net/manual/en/language.references.pass.php 它應該澄清我提供的解決方案.. 請務必在你的函數中添加&之前的參數。並刪除「$ idArray = array();」在你的循環中。 如果它仍然是15個索引,那麼也有可能,你的while子句「while(!empty($ html-> find('span [class = load-more-message]',0)));」只匹配一次。 – Yami

+0

是的,它使用你的推薦方法工作!謝謝 :) – DrDog

相關問題