1) I am not able to intersect arrays: 2) I am not able to get values of associative POST array using a key
$adbcols=Array ( [tcid] => tcid .... array containing database column names
$aformval=Array ( ['tcbcheck'] => on ['tcprice'] => 100 ['tctitf'] => 01
.. array containing POST data
function amatchfnc($a, $b) {
if ($a==$b){ return 0; }
return ($a>$b) ? 1:-1;
} // function amatchfnc($a, $b)
foreach ($_POST as $apie=>$aformval ) {
$aresf=array_intersect_ukey($aformval,$adbcols,"amatchfnc");
} }
MORE FULL CODE:
foreach ($_POST as $apie=>$aformval ) {
echo "<br><br> apie:"; print_r($apie);
echo "<br><br> aformval:"; print_r($aformval); //prints, also aformval[tcbcheck]
echo "<br><br> aformval[tcbcheck]".$aformval['tcbcheck']; //gives empy line
if(isset($_SESSION['vcurprcode'])) { $aformval['tcfkcode']=$_SESSION['vcurprcode'];}
$aresf=array_intersect_ukey($aformval,$adbcols,"amatchfnc");
echo "<br><br><br> print_r(aresf)="; print_r($aresf);
insertvalues($aresf,$adbcols, $conn, $vdbname, $tprice);
} //foreach ($_POST as $apie )
OUTPUT from prin_r, echo:
> apie:mon01
>
> aformval:Array ( ['tcbcheck'] => on ['tcprice'] => 100 ['tctitf'] =>
> 01 ['tcdatefromto'] => priceFrom20140101To20140131 ['tcbset'] => 3
> ['tcbsettit'] => month ['tcdesc'] => )
>
> aformval[tcbcheck]connectedstr02=SHOW COLUMNS FROM
> gintare_calendar.tapris3
> 0. ShowCol. Selected successfully gintare_calendar.tapris3
>
>
> print_r(adbcols)=Array ( [tcid] => tcid [tcdateacc] => tcdateacc
> [tcLdateacc] => tcLdateacc [tcdatecr] => tcdatecr [tcdesc] => tcdesc
> [tcbset] => tcbset [tcbsettit] => tcbsettit [tcbconf] => tcbconf
> [tcbcheck] => tcbcheck [tcdurup] => tcdurup [tcdurlow] => tcdurlow
> [tctitf] => tctitf [tcdate] => tcdate [tcdatefromto] => tcdatefromto
> [tccur] => tccur [tcbcur] => tcbcur [tcprice] => tcprice [tcbdisc] =>
> tcbdisc [tcfkdisc] => tcfkdisc [tcdisc] => tcdisc [tcdiscper] =>
> tcdiscper [tcpricedisc] => tcpricedisc [tcpurpose] => tcpurpose
> [tcdescpric] => tcdescpric [tcbsavset] => tcbsavset [tcprtit] =>
> tcprtit [tcfkcode] => tcfkcode [tcvauk] => tcvauk [tcdatefrom] =>
> tcdatefrom [tcdateto] => tcdateto [tcpryear] => tcpryear [tcprmonth]
> => tcprmonth [tcprweek] => tcprweek [tcprday] => tcprday )
>
>
> abefore intersect print_r(aformval)=Array ( ['tcbcheck'] => on
> ['tcprice'] => 100 ['tctitf'] => 01 ['tcdatefromto'] =>
> priceFrom20140101To20140131 ['tcbset'] => 3 ['tcbsettit'] => month
> ['tcdesc'] => [tcbcheck] => checked [tcfkcode] => 1 )
>
> print_r(aresf)=Array ( [tcbcheck] => checked [tcfkcode] => 1 )
$aresf
should contain all $aformal
values.
I make intersection just to check if column names of aref coincides with column names of the database. $adbcols i am getting from database schema.
THE HTML FILE which i POST contains the following paragraphs:
<input type= "text" name="mon01['tcprice']" value="<?php echo $mon01['tcprice']; ?>" " >
<input type="range" name="mon01[range]" min="0" max="1000" >
<input type="hidden" class="mon" name="mon01['tctitf']" value="01"; " size="1" >
<input type="hidden" class="mon" name="mon01['tcdatefromto']"
value="priceFrom20140101To20140131"; " size="1" >
<input type="hidden" class="mon" name="mon01['tcbset']" value="3"; " size="1" >
<input type="hidden" class="mon" name="mon01['tcbsettit']" value="month"; " >
<textarea cols="50" rows="3" name="mon01['tcdesc']"
value="<?php echo $mon01['tcdesc']; ?>" > </textarea>
GENERALLY array intersection works. The example below is with some arrays:
<?php
$af=array( "a"=>"a","c"=>"c", "e"=>"e" );
$ad=array( "a"=>"a","b"=>"b","c"=>"c","d"=>"d", "e"=>"e" );
$af=array_intersect_ukey($af,$ad,"amatchfnc");
echo "<br> print_r(af)="; print_r($aresf);
?>
gives : print_r(aresf)=Array ( [a] => a [c] => c [e] => e )
But if i try to intersect with POST array $aformval, i am getting strange results. How to intersect the POST array?
I am not completely sure about your requirement, but i think this what you want. You can try
array_merge()
. Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. Since youhave same key
in both array the key=> value of second array will be in the resultant array(in this case$aformval
)Result:
If you need to check whether all
$aformaval keys
are present in$adbcols
then tryif
count($arr) > 0
then you have keys in$aformval
that are not present in$adbcols
. You can see the difference by print_r($arr);