You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP多变量赋值为数组变量及$msg变量赋值修正问询

Hey there! Let's tackle your two PHP questions step by step:

1. Assigning multiple variables to a single array variable

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.

2. Fixing the $msg assignment error

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:39:03