我在比較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];
顯示完整的非工作代碼 – zavg
您發佈的代碼是確定 – zavg
與製造額外的缺碼 – MichaelP