我已經陣列$_POST
如下:我在做下面的數組操作時做錯了什麼?
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
)
我以保持數據以相等的索引一起,即索引1的數據應是在一個陣列操縱上述陣列,索引2的數據應該在另一個陣列,等等......它也在工作,但可應用的狀態正在變得不安。適用的狀態很常見。他們不屬於任何指數。如何避免這種情況?
$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);
}
}
}
}
上述操作之後,如果我打印是如下數組:
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
)
您可以從上面的數組,適用的狀態數組被幹擾觀察。我想避免這種干擾。你能糾正我在數組操作中犯的錯誤嗎?謝謝。