I've array $_POST
as follows:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[product_id_1] => Array
(
[1] => 9
[2] => 11
)
[pack] => Array
(
[1] => 10
[2] => 50
)
[quantity] => Array
(
[1] => 20
[2] => 60
)
[volume] => Array
(
[1] => 30
[2] => 70
)
[units] => Array
(
[1] => 12
[2] => 7
)
[amount] => Array
(
[1] => 40
[2] => 80
)
[product_id_2] => Array
(
[1] => 10
[2] => 8
)
[rebate_start_date] => 2014-05-28
[rebate_expiry_date] => 2014-05-31
[applicable_states] => Array
(
[0] => 2
[1] => 9
[2] => 16
[3] => 18
)
[multiselect] => 18
[rebate_total_count] => 8000
)
I'm manipulating above array in order to keep data with equal index together i.e. data of index 1 should be in one array, data of index 2 should be in another array, and so on... It's working also but the applicaable states are getting disturbed. Applicable states are common. They don't belong to any index. How to avoid this?
$rebate_by_product = array();
foreach ($_POST as $key => $val) {
if (!is_array($val)) {
$rebate_by_product[$key] = $val;
} elseif (preg_match('/^product_id_(\d+)$/', $key, $match)) {
$i = $match[1];
if (isset($rebate_by_product[$i])) {
$rebate_by_product[$i][$key] = $val;
} else {
$rebate_by_product[$i] = array($key => $val);
}
} else {
foreach ($val as $i => $subval) {
if (isset($rebate_by_product[$i])) {
$rebate_by_product[$i][$key] = $subval;
} else {
$rebate_by_product[$i] = array($key => $subval);
}
}
}
}
After above manipulation if I print the array it's as follows:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[1] => Array
(
[product_id_1] => Array
(
[1] => 9
[2] => 11
)
[pack] => 10
[quantity] => 20
[volume] => 30
[units] => 12
[amount] => 40
[applicable_states] => 9
)
[2] => Array
(
[pack] => 50
[quantity] => 60
[volume] => 70
[units] => 7
[amount] => 80
[product_id_2] => Array
(
[1] => 10
[2] => 8
)
[applicable_states] => 16
)
[rebate_start_date] => 2014-05-28
[rebate_expiry_date] => 2014-05-31
[0] => Array
(
[applicable_states] => 2
)
[3] => Array
(
[applicable_states] => 18
)
[multiselect] => 18
[rebate_total_count] => 8000
)
You can observe from above array that the array of applicable states is disturbed. I want to avoid that disturbance. Can you please correct the mistake I'm making in array manipulation? Thanks.