I am not able to intersect arrays. POST, PHP, How to intersect the POST array?

113 Views Asked by At

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?

3

There are 3 best solutions below

3
On

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 you have same key in both array the key=> value of second array will be in the resultant array(in this case $aformval)

$adbcols = array('tcid'=>'tcid','tcbcheck'=>'tcbcheck');
$aformval = array('tcprice'=>'100','tcbcheck'=>'on','tctitf'=>0,'tcid'=>1);
$arr = array_merge($adbcols,$aformval);
print_r($arr);

Result:

Array
(
    [tcid] => 1
    [tcbcheck] => on
    [tcprice] => 100
    [tctitf] => 0
)

If you need to check whether all $aformaval keys are present in $adbcols then try

$arr =array_diff(array_keys($aformval),array_keys($adbcols));

if count($arr) > 0 then you have keys in $aformval that are not present in $adbcols. You can see the difference by print_r($arr);

0
On

Use array_merge https://www.php.net/array_merge

$result = array_merge($array1, $array2);
0
On

The reason was that i was intersecting $aformval["'key'"] with $dbcols["key"]

OR i have to make html form without quates:

<input type= "text" name="mon01[tcprice]" value="<?php echo $mon01[tcprice]; ?>"   > 

OR i have to add quates to adbcols keys, i.e. use $adbcols["'tcprice'"].

 <input type= "text" name="mon01['tcprice']" value="<?php echo $mon01['tcprice']; ?>"   >