mysql - convert variable names to array in PHP -
i have lite problem converting php $_post
virables.
i send such information website:
$_post['name.1']='xxx'; $_post['height.1']='100'; $_post['qty.1']='2'; $_post['op.1.0']='color'; $_post['op.1.1']='size'; $_post['opv.1.0.0']='red'; $_post['opv.1.0.1']='blue'; $_post['opv.1.1.0']='xl'; $_post['opv.1.1.1']='l'; $_post['opv.1.1.2']='xxl'; $_post['name.2']='yyy'; $_post['height.2']='10'; $_post['qty.2']='4'; $_post['number.2']='4'; $_post['op.2.0']='color'; $_post['op.2.1']='weight'; $_post['opv.2.0.0']='red'; $_post['opv.2.0.1']='silver'; $_post['opv.2.1.0']='90'; $_post['opv.2.1.1']='60'; $_post['opv.2.1.2']='42';
i need convert data format:
$product[1]['name']='xxx'; $product[1]['height']='100'; $product[1]['qty']='2'; $product[1]['op'][0]='color'; $product[1]['op'][1]=size; $product[1]['opv'][0][0]='red'; $product[1]['opv'][0][1]='blue'; $product[1]['opv'][1][0]='xl'; $product[1]['opv'][1][1]='l'; $product[1]['opv'][1][2]='xxl'; $product[2]['name']='yyy'; $product[2]['height']='10'; $product[2]['qty']='2'; $product[2]['number']='2'; $product[2]['op'][0]='color'; $product[2]['op'][1]='weight'; $product[2]['opv'][0][0]='red'; $product[2]['opv'][0][1]='silver'; $product[2]['opv'][1][0]='90'; $product[2]['opv'][1][1]='60'; $product[2]['opv'][1][2]='42'; first .1 product nr. second .1 option nr. third .1 option value.
$product = array(); foreach ($_post $key => $value) { $key_array = explode('.', $key); $name = array_shift($key_array); $subscript = array_shift($key_array); $target =& $product[$subscript][$name]; // if there remaining elements, they're subscripts nested array levels foreach ($key_array $next_subscript) { $target =& $target[$next_subscript]; } $target = $value; }
Comments
Post a Comment