我有以下帖子数据,这些数据会根据输入的数量而增加,从而产生动态帖子名称。
Array ( [VAL0] => TESTING [UNIT0] => 99 [VAL1] => TESTING1 [UNITS1] => 88 )
我正在完成对帖子数据的循环,以找到VAL
和UNIT
帖子,并将它们分配给变量然后分组。
除了一组值之外,它的工作正在重复。
任何人都可以看到我出错的地方吗?
$bulkProducts = array();
foreach ($_POST as $key => $value) {
if (strpos($key, "VAL")===0) {
$val= $value;
}
if (strpos($key, "UNIT")===0) {
$newunits= $value;
}
if (isset($val,$newunits)) {
$products = array();
$products = array('VAL' => $val,
'UNITS' =>$newunits
);
array_push($bulkProducts,$products);
}
}
print_r($bulkProducts);
打印结果:
Array ( [0] => Array ( [VAL] => TESTING [UNITS] => 99 ) [1] => Array ( [VAL] => TESTING1 [UNITS] => 88 ) [2] => Array ( [VAL] => TESTING1 [UNITS] => 88 ) )
答案 0 :(得分:1)
在此处更改:
if (isset($val,$newunits)) {
$products = array('VAL' => $val,
'UNITS' =>$newunits
);
$val = NULL; $newunits = NULL;
array_push($bulkProducts,$products);
}