2013-12-17 49 views
1

我在比較2個數組並輸出單獨數組中的差異。將數組與多於一行進行比較

我做了以下代碼行來比較2數組;

$INPUTdifference = array_udiff($userINPUT, $iptablesINPUT, function ($userINPUT, $iptablesINPUT) { return (int) ($userINPUT != $iptablesINPUT); }); 

userINPUT包含測試一行,並iptablesINPUT幾下,它完美的作品。

但是,無論何時在userINPUT數組中添加第二行,它都會完全停止工作。它甚至沒有給我什麼,但我打印INPUTdifference anymore

這裏有什麼問題?

編輯;

獲取userINPUT的方式;

function getIPTablesINPUT() { 


    /* ------------------------------ */ 
    // INPUT CHAIN 
    /* ------------------------------ */ 



    $INPUTCOMMAND = popen('/usr/bin/sudo /sbin/iptables -L INPUT -nvx --line-numbers | tail -n +3', 'r'); 
    $INPUTcontent = ''; 
    while (!feof($INPUTCOMMAND)) { 
     $INPUTcontent .= fread($INPUTCOMMAND, 4096); 
    } 
    pclose($INPUTCOMMAND); 
    $INPUTlines = explode("\n", trim($INPUTcontent)); 
    $INPUTresults = array(); 
    $INPUTcounter = 0; 


    foreach ($INPUTlines as $line) { 


     $segments = preg_split('/[\s]+/', $line); 


     $INPUTArray = array(
      'num' => $segments[0], 
      'pkts' => $segments[1], 
      'bytes' => $segments[2], 
      'target' => $segments[3], 
      'prot' => $segments[4], 
      'opt' => $segments[5], 
      'in' => $segments[6], 
      'out' => $segments[7], 
      'source' => $segments[8], 
      'destination' => $segments[9] 
     ); 


     array_push($INPUTresults, $INPUTArray); 

     $INPUTcounter++; 
    } 

    return $INPUTresults; 
} 

然後,該函數的外

$iptablesINPUT = getIPTablesINPUT(); 

然後,在數據的規則

$dbconnection = pg_connect("host=x port=x dbname=x user=x password=xxxx") or die("Unable to connect to Postgres"); 


    // INPUT table from userDB 
    $userINPUTresult = pg_query($dbconnection, "SELECT * FROM \"INPUT\""); 
    if (pg_affected_rows($userINPUTresult) === 1) { 

     $userINPUTArray = pg_fetch_all($userINPUTresult); 
     echo "INPUT CHAIN RULES LOADED \n"; 
    } else { 

     echo ("NO INPUT CHAIN RULES \n"); 
    } 

==== VAR轉儲====

的方式var_dump $ iptablesINPUT

NULL 
array(2) { 
    [0]=> 
    array(10) { 
    ["num"]=> 
    string(1) "1" 
    ["pkts"]=> 
    string(1) "0" 
    ["bytes"]=> 
    string(1) "0" 
    ["target"]=> 
    string(4) "DROP" 
    ["prot"]=> 
    string(3) "all" 
    ["opt"]=> 
    string(2) "--" 
    ["in"]=> 
    string(1) "*" 
    ["out"]=> 
    string(1) "*" 
    ["source"]=> 
    string(9) "192.0.0.1" 
    ["destination"]=> 
    string(9) "0.0.0.0/0" 
    } 
    [1]=> 
    array(10) { 
    ["num"]=> 
    string(1) "2" 
    ["pkts"]=> 
    string(1) "0" 
    ["bytes"]=> 
    string(1) "0" 
    ["target"]=> 
    string(4) "DROP" 
    ["prot"]=> 
    string(3) "all" 
    ["opt"]=> 
    string(2) "--" 
    ["in"]=> 
    string(1) "*" 
    ["out"]=> 
    string(1) "*" 
    ["source"]=> 
    string(9) "192.0.0.2" 
    ["destination"]=> 
    string(9) "0.0.0.0/0" 
    } 
} 

的var_dump $ userINPUT

NULL 

Apparentely這是哪裏出了差錯現場...

==== EDIT 2 ======

這是我的方式從該範圍外的函數中提取數組。

// Slice up userDB arrays for comparing 
$userINPUT = $allRules[0]; 
$userOUTPUT = $allRules[1]; 
$userFORWARD = $allRules[2]; 
$userPOSTROUTING = $allRules[3]; 
$userPREROUTING = $allRules[4]; 
+0

顯示完整的非工作代碼 – zavg

+0

您發佈的代碼是確定 – zavg

+0

與製造額外的缺碼 – MichaelP

回答

1

array_udiff函數返回第一個數組中不在下一個數組中的元素。

array_udiff page

陣列array_udiff(數組$數組1,數組$數組2 [,數組$ ...],可調用$ value_compare_func)

返回包含ARRAY1的所有值的數組中不存在任何的其他參數

由於您的第一陣列的值($userINPUT你的情況)是null那麼array_udiff的結果也將是null

看,下面的代碼

$a = array(1,2); 
$b = array(1); 
$c = array_udiff($a, $b, function($a, $b){return (int) $a != $b;}); 

// $c == array(2) now 

執行後的$c值將是array(2),因爲下面的代碼

$a = null; 
$b = array(1); 
$c = array_udiff($a, $b, function($a, $b){return (int) $a != $b;}); 

// $c == null now 

會導致$c等於null

+0

感謝zavg函數的解釋,我現在理解它的原理。然而,這裏的問題是**爲什麼** $ userINPUT在用於'array_diff'之前是空的 – MichaelP

+0

這是另一個問題:)據我所知,你從postgres中獲取它,可能是你的查詢沒有執行。 – zavg

+0

getIPTables()在其作用域中有正確的變量。但是,我需要將結果數組從範圍中取出,並通過提取數組的每個條目來完成此操作。我將添加代碼,看看。 – MichaelP