Reputation: 149
I have the following post data which increments based on the number of inputs resulting in a dynamic post name.
Array ( [VAL0] => TESTING [UNIT0] => 99 [VAL1] => TESTING1 [UNITS1] => 88 )
I'm completing a loop on the post data to find the VAL
and UNIT
posts and assign them to an variable and then array.
its working except for 1 set of values are being duplicated.
can anyone see where i am going wrong?
$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);
print r result :
Array ( [0] => Array ( [VAL] => TESTING [UNITS] => 99 ) [1] => Array ( [VAL] => TESTING1 [UNITS] => 88 ) [2] => Array ( [VAL] => TESTING1 [UNITS] => 88 ) )
Upvotes: 0
Views: 65
Reputation: 314
change here:
if (isset($val,$newunits)) {
$products = array('VAL' => $val,
'UNITS' =>$newunits
);
$val = NULL; $newunits = NULL;
array_push($bulkProducts,$products);
}
Upvotes: 1