我无法与数组相交。 POST,PHP,如何与 POST 数组相交?
Posted
技术标签:
【中文标题】我无法与数组相交。 POST,PHP,如何与 POST 数组相交?【英文标题】:I am not able to intersect arrays. POST, PHP, How to intersect the POST array? 【发布时间】:2014-01-21 07:09:45 【问题描述】: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 数组相交?
【问题讨论】:
【参考方案1】:使用array_merge http://in1.php.net/array_merge
$result = array_merge($array1, $array2);
【讨论】:
【参考方案2】:我不完全确定您的要求,但我认为这是您想要的。你可以试试array_merge()
。将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。由于您 have same 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); 看到区别
【讨论】:
我不需要附加值。我只想检查 $aformal 键是否对应于数据库列名。 $adbcols 包含数据库列名作为键和值:) 我从 mysql 数据库模式中获取它。 交集不适用于 POST 数组。如果我手动编写数组,它会起作用。也许有一些技巧必须如何处理帖子? 请阅读上面的答案。原因是我将 $aformval["'key'"] 与 $dbcols["key"] 相交。解决办法是改html表单。【参考方案3】:原因是我将$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']; ?>" >
【讨论】:
以上是关于我无法与数组相交。 POST,PHP,如何与 POST 数组相交?的主要内容,如果未能解决你的问题,请参考以下文章