0

1)我无法相交数组:2)我无法使用键获取关联 POST 数组的值

$adbcols=Array ( [tcid] => tcid .... array containing database column names

$aformval=Array ( ['tcbcheck'] => on ['tcprice'] => 100 ['tctitf'] => 01 

.. 包含 POST 数据的数组

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");
} }

更完整的代码:

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 ) 

来自 prin_r 的输出,回显:

> 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应该包含所有$aformal值。我进行交集只是为了检查 aref 的列名是否与数据库的列名一致。$adbcols 我从数据库模式中获取。

我发布的 HTML 文件包含以下段落:

<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>

通常数组交集有效。下面的例子是一些数组:

<?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);
?>

给出: print_r(aresf)=Array ( [a] => a [c] => c [e] => e )

但是如果我尝试与 POST 数组 $aformval 相交,我会得到奇怪的结果。如何与 POST 数组相交?

4

3 回答 3

0

原因是我$aformval["'key'"]$dbcols["key"]

或者我必须制作不带引号的 html 表单:

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

或者我必须向 adbcols 键添加 quates,即使用 $adbcols["'tcprice'"]。

 <input type= "text" name="mon01['tcprice']" value="<?php echo $mon01['tcprice']; ?>"   > 
于 2014-01-21T08:30:43.617 回答
0

我不完全确定您的要求,但我认为这是您想要的。你可以试试array_merge()。将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。由于您have same key在两个数组中,第二个数组的 key=> 值将在结果数组中(在这种情况下$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);

结果:

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

如果您需要检查是否所有$aformaval keys都存在$adbcols然后尝试

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

如果count($arr) > 0那么您有在$aformval中不存在的键$adbcols。您可以通过 print_r($arr); 看到差异

于 2014-01-21T07:22:28.403 回答
0

使用 array_merge http://in1.php.net/array_merge

$result = array_merge($array1, $array2);
于 2014-01-21T07:43:50.380 回答