2014-03-31 18 views
0

我有一個循環中的數據流。這從數據庫中逐行檢索結果。但是,我想要在流和給定數組之間生成一個array-diff形式。給定的數組是一個排除列表,說黑名單用戶的列表。所以這就是我迄今爲止的情況。如何在PHP中執行數據流和列表(排除列表)之間的設置差異

這是一個虛構的例子。我不想寫所有的數據庫代碼檢索行結果進行逐行

database objects containing ("Volvo","BMW","Toyota"); //assume that this is a continous stream of data from a database where we don't know the total elements. Just that single element 
$toys=array("BMW","Toyota");   //assume that this is a list of blacklisted users 
for($rindex=0; $rindex < count($cars); $rindex++) 
{ 

    for($index=0; $index < size(database objects); $index++) 
    { 
     //obtain the database object on a row by row basis 
     if (strcmp ($cars[$rindex] , $toys[$index])!=0) //specify exclusion 
     { 
     echo $cars[$rindex]; 

    } 
    } 
} 

預期的答案應該是Volvo。如果我們知道$cars中的所有元素,就可以輕鬆完成此操作,因此我們不能使用array-diff($cars,$toys)。假設$cars是一個連續的數據流,表示逐行的數據庫結果。我們如何能固定的已知黑名單陣列和數據的連續流

記住array-diff()不能用,因爲我們不知道數據流的全尺寸或流中的所有元素,我們開始之間做array-diff()

這可以使用array-filter()完成,但我已經有性能問題並希望在線執行此操作。

請在PHP中提供答案。

+0

1.'in_array' 2.'foreach'「我們不知道總的元素」 - 然後你使用'count($ cars)'? :-D – zerkms

+0

問:這是作業,對嗎?問:兩個輸入/兩個輸入是否分類?問:你允許使用[PHP in_array()](http://us1.php.net/in_array)嗎? – FoggyDay

+0

這不是作業。我是PHP的新手,並試圖完成一個項目的一部分。 –

回答

0

快速查找數組 - 代碼被註釋和測試。 PHP 5.3.18

<?php // Q22754093 

// database objects containing ("Volvo","BMW","Toyota"); //assume that this is a continous stream of data from a database where we don't know the total elements. Just that single element 
$allToys = array("Volvo","BMW","Toyota"); 


// use the 'toy' as the array key -- there is no faster way of checking whether 
// something is in an array, especially if the array is large. 
$badToys = array("BMW"  => true, 
       "Toyota" => true);   //assume that this is a list of blacklisted users 
               // by setting them false then you treat them as 
              // 'good' if you wish - whatever 


$checkedGoodToys = array(); 
$checkedBadToys = array(); 

//obtain the database object on a row by row basis 
foreach($allToys as $oneToy) 
{ 
    if (isBadToy($oneToy)) { 
     // do what you want with the bad toy... 
     $checkedBadToys[] = $oneToy; 
    } 
    else { 
     // do what you want with the good toy... 
     $checkedGoodToys[] = $oneToy; 
    } 
} 

var_dump($checkedGoodToys, 'The Good Toys'); 
var_dump($checkedBadToys, 'The Bad Toys'); 
exit; 

// check if the toy is in the badToy array 
function isBadToy($theToy) 
{ 
    global $badToys; 

    return isset($badToys[$theToy]) && $badToys[$theToy]; 
} 
0

in_array()訣竅。謝謝你的答案。

database objects containing ("Volvo","BMW","Toyota"); //assume that this is a continous stream of data from a database where we don't know the total elements. Just that single element 
$toys=array("BMW","Toyota");   //assume that this is a list of blacklisted users 
for($rindex=0; $rindex < count($cars); $rindex++) 
{ 

    for($index=0; $index < size(database objects); $index++) 
    { 
     //obtain the database object on a row by row basis 
     if (in_array($cars[$rindex] , $toys) //specify exclusion 
     { 
      echo $cars[$rindex]; 

     } 
    } 
} 
+0

對於大型陣列來說它會很慢,因爲平均一半的陣列將不得不被檢查。檢查密鑰(索引)是否存在對於大型數組是非常快速和線性的。基本上,成本是哈希計算,然後尋找哈希列表,通常是內存中的一個查找。 –

相關問題