如何从 php post 创建多维数组?

How to create multidimensional array from php post?

如何从 php post 创建多维和关联数组?

        $html .= '<td><input id="location" name="location['.$i.']" type="text" class="form-control form-control-sm form-control-solid" placeholder="result data (kosongkan jika tidak diperlukan)" value=""/></td>';
        $html .= '<td><input id="msg" name="msg['.$i.']" type="text" class="form-control form-control-sm form-control-solid" value="' . htmlspecialchars($key) . '"/></td>';
        $html .= '<td><input id="order_id" name="order_id['.$i.']" type="text" class="form-control form-control-sm form-control-solid" value="' . htmlspecialchars($value) . '"/></td>';

我想像这样输出数组:

[$_POST['location'] => [$_POST['msg']=>$_POST['order_id']]]

你犯了一些简单的错误

你可以foreach一个数组然后你不关心它有多大或多小。您在 $_POST.

中使用的变量也有错误

使用 foreach( $arr as $index => $value) 语法为您获取正在处理的数组的索引,如果所有 3 个数组的大小始终相同,则您可以使用它来引用其他数组。

$arr = [];
foreach ( $_POST['location'] as $i => $locn ) {
    $arr[$locn][] = [ $_POST['msg'][$i] => $_POST['order_id'][$i] ];
}

如果您

,它有助于调试您的代码

Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. <?php error_reporting(E_ALL); ini_set('display_errors', 1);