为什么这不起作用?
// I'm sending this data: $_POST['amount_1'] = '3';
$val = '1'; // This is coming from a foreach loop, but I'm specifying here for simplicity
echo $_POST['amount_'.$val]; // Returns null
echo $_POST['amount_1']; // Returns 3
尝试获取 POST 值时是否不允许使用变量?
编辑:每个请求发布更多代码。
$outgoing = array();
foreach($_POST as $key => $val):
if(strpos($key,'_number_')){ // Matching 'item_number_' would return false because of 0-indexing
$item_id = $val;
$test = str_replace('item_number_',$key); // Realized when pasting this that I didn't have a $replacement defined.
$quantity = $_POST['quantity_'.$test];
$name = $_POST['lp-name'];
$email = $_POST['lp-email'];
$phone = $_POST['lp-phone'];
array_push($outgoing,array($test,$item_id,$quantity,$name,$email,$phone));
}
endforeach;
$json = json_encode($outgoing);
现在工作。我的 str_replace() 中没有$replacement
定义。