2010-12-15 71 views
0

輸出保存在PHP從jquery.serilizeArray()PHP分裂陣列分成若干小組,在MySQL經由的print_r

Array ( 
    [pnr_item_2] => 2 
    [pkt_item_2] => Hosting L 
    [desc_item_2] => Domain + Hosting 
    [qty_item_2] => 9 
    [price_item_2] => 12.4 
    [pnr_item_1] => 1 
    [pkt_item_1] => HostingXXL 
    [desc_item_1] => 20GB, 1x.de 
    [qty_item_1] => 2 
    [price_item_1] => 15.5 
    [pnr_item_3] => 3 
    [pkt_item_3] => Domain 
    [desc_item_3] => Standarddomain 
    [qty_item_3] => 6 
    [price_item_3] => 5 
    [pnr_item_4] => 3 
    [pkt_item_4] => Domain 
    [desc_item_4] => Standarddomain 
    [qty_item_4] => 7 
    [price_item_4] => 5 
    [action] => save 
    [mysql] => update 
    [total] => 351.1 
) 

確實存在的可能性通過使用_item_##分裂成組的數組-數? 我想將項目保存到我的第一個MySQL表中,action是executet函數,我通過$_POST[action][total]得到的函數必須保存在額外的(第二個)MySQL表中。

+0

呵呵?請修改問題... – Jakub 2010-12-15 14:44:25

回答

1

根據我在這個問題的意思猜測;)

//$yourArr = array(all,your,input); 

$action = $yourArr['action']; 
$mysql = $yourArr['mysql']; 
$total = $yourArr['total']; 
unset($yourArr['action'] , $yourArr['mysql'] , $yourArr['total']); 

foreach ($yourArr as $k => $v) { 
    list($type,$num) = explode('_item_',$k); 
    $items[$num][$type] = $v; 
} 

比你有,例如:

$items = array(
    [2]['pnr'] => 2, 
    [2]['pkt'] => 'Hosting L', 
    [2][['desc_item'] => 'Domain + Hosting' 
    ... 
); 

免責聲明:我沒有實際測試此

+0

作品fin,thx !! – hoerf 2010-12-17 20:43:26

0

您可以循環在陣列上:

$items = array(); 
foreach($array as $key=>$value) { 
    if(stripos($key, '_item_' !== false)) { 
     $items[$key] = $value; 
     unset($array[$key]); 
    } 
} 

然後$items包含有在關鍵_item_$array其餘的所有元素。

但更容易的是先保存total並從陣列中刪除action,mysqltotal。所以只是顛倒這兩個操作。