php split Array via [name] into groups to save in MySQL
output via print_r in php from jquery.serilizeArray()
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
)
Does exist a possibility to split an Array in groups by using the _item_##
-number?
I want to save items to my first MySQL table, action is the execute开发者_如何学Ct function, which I get via $_POST[action]
and [total]
have to be saved in a extra (2nd) MySQL-table.
Based on my guess at the question's meaning ;)
//$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;
}
Than you have, for example:
$items = array(
[2]['pnr'] => 2,
[2]['pkt'] => 'Hosting L',
[2][['desc_item'] => 'Domain + Hosting'
...
);
disclaimer: I didn't actually test this
You can loop over the array:
$items = array();
foreach($array as $key=>$value) {
if(stripos($key, '_item_' !== false)) {
$items[$key] = $value;
unset($array[$key]);
}
}
Then $items
contain all elements that have _item_
in the key and $array
the rest.
But easier would be to save total
first and remove action
, mysql
and total
from the array. So just reversing both operations.
精彩评论