2014-01-21 21 views
0

1)我不能交叉陣列: 2)我不能夠使用密鑰我無法與數組相交。 POST,PHP,如何交叉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"); 
} } 

MORE得到締POST數組的值全碼:

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文件我POST包含以下段落:

<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陣列相交的作品。下面的例子是與某些陣列:

<?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)=陣列([α] =>一個並[c] => C [E] => E)

但是,如果我嘗試與POST數組$ aformval相交,我得到了奇怪的結果。如何交叉POST數組?

回答

0

我不完全確定你的要求,但我認爲這是你想要的。你可以試試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,那麼你在有鑰匙不存在於$adbcols中。您可以通過print_r($ arr)查看差異;

+0

我不需要追加值。我只想檢查$ aformal鍵對應於數據庫列名稱。 $ adbcols包含數據庫列名作爲鍵和值:)我從MySQL數據庫模式中獲取它。 –

+0

交集不適用於POST數組。它適用於我手寫數組。也許有一些技巧需要處理? –

+0

請閱讀上面的答案。原因是我與$ dbcols [「key」]相交$ aformval [「'key'」]。解決方案是更改html表單。 –

0

的原因是,我是相交$aformval["'key'"]$dbcols["key"]

或者我必須做出HTML表單不quates:

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

或者我要補充前往adbcols鍵,即使用$ adbcols [「'tcprice'」]。

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