PHP多变量赋值为数组变量及$msg变量赋值修正问询
Hey there! Let's tackle your two PHP questions step by step:
If you want to group your related POST variables into one array for easier management, you can create an associative array like this:
// Group all well-related variables into a single array $wellRecords = [ 'pn' => $_POST['pn'], 'ln' => $_POST['ln'], 'logn' => $_POST['logn'], 'wellno' => $_POST['wellno'] ];
This way, you can access each dataset later using $wellRecords['pn'][$i], which keeps your code cleaner and more organized.
Your original code has a syntax issue: echo is a language construct for outputting content, not something you can use in an assignment statement. To make $msg a usable variable for sending messages, you just need to directly concatenate your strings without echo.
Here's the corrected code (using the grouped array from above for clarity):
// First, make sure $chkcount is set correctly (e.g., using the length of one of the arrays) $chkcount = count($wellRecords['wellno']); for ($i = 0; $i < $chkcount; $i++) { // Correctly assign the concatenated string to $msg $msg = $wellRecords['wellno'][$i] . ':' . $wellRecords['ln'][$i] . '/' . $wellRecords['pn'][$i] . '-' . $wellRecords['logn'][$i]; // Optional: Output the message to the browser echo $msg . "<br>"; // Now you can use $msg for your messaging logic! // Example: Send via email // mail('admin@yourdomain.com', 'Well Update', $msg); // Example: Save to a log file // file_put_contents('well_logs.txt', $msg . PHP_EOL, FILE_APPEND); }
If you prefer to keep your original individual variables instead of grouping them, here's the corrected version without the array:
$pn = $_POST['pn']; $ln = $_POST['ln']; $logn = $_POST['logn']; $wellno = $_POST['wellno']; for ($i = 0; $i < $chkcount; $i++) { $msg = $wellno[$i] . ':' . $ln[$i] . '/' . $pn[$i] . '-' . $logn[$i]; echo $msg . "<br>"; // Use $msg for your messaging needs here }
The key fix is removing all instances of echo from the assignment line — $msg will hold the full string, which you can then use for any messaging task you need.
内容的提问来源于stack exchange,提问作者user3521051

