Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing Array with more than one row
    primarykey
    data
    text
    <p>I'm trying to compare 2 Arrays and output the difference in a separate array.</p> <p>I've made the following line of code to compare 2 Arrays;</p> <pre><code>$INPUTdifference = array_udiff($userINPUT, $iptablesINPUT, function ($userINPUT, $iptablesINPUT) { return (int) ($userINPUT != $iptablesINPUT); }); </code></pre> <p>When <code>userINPUT</code> contains one row for testing, and <code>iptablesINPUT</code> a few, it works flawlessly.</p> <p>However, whenever I add a second row in the userINPUT array, it completely stops working. It doesn't even give anything while I print the <code>INPUTdifference anymore</code></p> <p>What is wrong here?</p> <p>EDIT;</p> <p>The way userINPUT is acquired;</p> <pre><code>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' =&gt; $segments[0], 'pkts' =&gt; $segments[1], 'bytes' =&gt; $segments[2], 'target' =&gt; $segments[3], 'prot' =&gt; $segments[4], 'opt' =&gt; $segments[5], 'in' =&gt; $segments[6], 'out' =&gt; $segments[7], 'source' =&gt; $segments[8], 'destination' =&gt; $segments[9] ); array_push($INPUTresults, $INPUTArray); $INPUTcounter++; } return $INPUTresults; } </code></pre> <p>then, outside of the function</p> <pre><code>$iptablesINPUT = getIPTablesINPUT(); </code></pre> <p>Then, the way the rules in the data</p> <pre><code>$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"); } </code></pre> <p>==== VAR DUMPS ====</p> <p>var_dump $iptablesINPUT</p> <pre><code>NULL array(2) { [0]=&gt; array(10) { ["num"]=&gt; string(1) "1" ["pkts"]=&gt; string(1) "0" ["bytes"]=&gt; string(1) "0" ["target"]=&gt; string(4) "DROP" ["prot"]=&gt; string(3) "all" ["opt"]=&gt; string(2) "--" ["in"]=&gt; string(1) "*" ["out"]=&gt; string(1) "*" ["source"]=&gt; string(9) "192.0.0.1" ["destination"]=&gt; string(9) "0.0.0.0/0" } [1]=&gt; array(10) { ["num"]=&gt; string(1) "2" ["pkts"]=&gt; string(1) "0" ["bytes"]=&gt; string(1) "0" ["target"]=&gt; string(4) "DROP" ["prot"]=&gt; string(3) "all" ["opt"]=&gt; string(2) "--" ["in"]=&gt; string(1) "*" ["out"]=&gt; string(1) "*" ["source"]=&gt; string(9) "192.0.0.2" ["destination"]=&gt; string(9) "0.0.0.0/0" } } </code></pre> <p>var_dump $userINPUT</p> <pre><code>NULL </code></pre> <p>Apparentely this is the spot where something goes wrong...</p> <p>==== EDIT 2 ======</p> <p>This is the way I extract the arrays from the function out of the scope.</p> <pre><code>// Slice up userDB arrays for comparing $userINPUT = $allRules[0]; $userOUTPUT = $allRules[1]; $userFORWARD = $allRules[2]; $userPOSTROUTING = $allRules[3]; $userPREROUTING = $allRules[4]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload