2014-01-13 3 views
0

이 코드의 끝에는 모든 배열에 모든 $ id를주고 추가하는 배열이 있습니다.루프의 끝에 배열에 추가

코드는 배열에서 15가되고 다음 15 개 항목으로 덮어 씁니다. 배열에 30 개의 항목이있는 코드의 끝에 배열을 만들 수 있기를 원합니다. 아래

내 코드 : 당신은 여기 $ idArray을 재정의하기 때문에

$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'를 함수 정의 밖으로 끌어 와서 전역/세션으로 만들 수 있습니다. ariable. –

답변

1

이있다 :

$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

나는 여전히 어레이를 얻으려고 제안했다. 어레이 ([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] => 326880 [14 ] => 903301)'Array ([0] => 771733 [1] => 771713 [2] => 710078 [3] => 710056 [4] => 710105 [5] => 773022 [6] => > 271594 [7] => 323003 [8] => 9174020 [9] => 910406 ​​[10] => 65580 [11] => 19474 [12] => 908886 [13] => 64737 [14] => 306117)'다음 루프 이후. 아직도 그들을 함께 추가하지 않습니다 – 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