【问题标题】:Pair together two arrays in PHP after posting from two fields in a form从表单中的两个字段发布后,在 PHP 中将两个数组配对
【发布时间】:2011-09-06 11:51:38
【问题描述】:

我在一个表单(ip:port)中有两个字段。我想将它们加在一起形成一个字符串,例如 127.0.0.1:11111 以输入数据库。目前我有这个表格。

        <ul id="textlist">
            <li>IP: <input type="text" maxlength="15" size="15" name="ip[]" value="" />:<input type="text" name="port[]" maxlength="5" size="5" value="27015" /></li>
        </ul>

提交到这个进行解析。

$ip = array();
foreach ($_POST['ip'] as &$value) {
    if ($value != "") {
            if (preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/", $value)) {
                $ip[] = addslashes(htmlentities($value));
            } else {
                $error = 'Invalid IP Address. Please go back and try again.';
            }
    }
}

$port = array();
foreach ($_POST['port'] as &$value) {
        if ($value != "") {
                $port[] = addslashes(htmlentities($value));
        }
    }
}

然后,我最终得到 2 个 ip 和端口数组(每个数组最多 5 个),我想在添加到数据库之前将它们成对连接在一起。

感谢您的帮助!

【问题讨论】:

    标签: php html arrays append field


    【解决方案1】:

    您可以通过稍微重新格式化您的表单来让自己的生活更轻松。您可以为您的 ip 和 port 字段提供显式索引,而不是依赖 [] 行为,或者您可以将每个 ip 和 port 捆绑在一起成为更大数组的子字段。

    (以下示例中省略了示例中不重要的属性和其他内容)

    选项a:

    <li><input name="ip[1]" />: <input name="port[1]" /></li>
    <li><input name="ip[2]" />: <input name="port[2]" /></li>
    <li><input name="ip[3]" />: <input name="port[3]" /></li>
    <!-- etc -->
    

    这应该返回两个数组,一个 IP 数组和一个端口数组,每行的索引相同。然后,您可以使用foreach ($ip as $key =&gt; $val) 并使用 $key 中的值从 Port 数组中获取相应的值。

    选项 b:

    <li><input name="connection[][ip]" />: <input name="connection[][port]" /></li>
    <li><input name="connection[][ip]" />: <input name="connection[][port]" /></li>
    <li><input name="connection[][ip]" />: <input name="connection[][port]" /></li>
    <!-- etc -->
    

    这应该返回一个数组,每一行包含一个捆绑在一起的 IP 和端口。然后,您可以只对数组进行 foreach 。

    foreach ($connection as $row)
    {
        var_dump ($row ['ip'], $row ['port']);
    }
    

    【讨论】:

      【解决方案2】:

      最好在 HTML 中添加数字索引,以确保您有正确的配对。

      <ul id="textlist">
          <li>IP: <input type="text" maxlength="15" size="15" name="ip[0]" value="" />:<input type="text" name="port[0]" maxlength="5" size="5" value="27015" /></li>
          <li>IP: <input type="text" maxlength="15" size="15" name="ip[1]" value="" />:<input type="text" name="port[1]" maxlength="5" size="5" value="27015" /></li>
      </ul>
      

      之后,您可以安全地一次循环。

      $pairs = array();
      foreach ( $_POST['ip'] as $i => $ip ) 
      {
          if ( preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/", $ip) && $_POST['port'][$i] != "" ) 
          {
              $pairs[] = sprintf('%s:%s', addslashes(htmlentities($value)), addslashes(htmlentities($_POST['port'][$i])));
          } 
      }
      

      添加一些额外的错误检查是明智的,例如检查是否设置了$_POST['port'][$i],其值等。

      【讨论】:

        【解决方案3】:
        $x = 0
        $ip_port = array();
        foreach ($_POST['ip'] as &$value) {
            if ($value != "") {
                    if (preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/", $value)) {
                        $ip_port[$x++]['ip'] = addslashes(htmlentities($value));
                    } else {
                        $error = 'Invalid IP Address. Please go back and try again.';
                    }
            }
        }
        
        $x = 0
        foreach ($_POST['port'] as &$value) {
                if ($value != "") {
                        $ip_port[$x++]['port'] = addslashes(htmlentities($value));
                }
            }
        }
        
        foreach ($ip_post as $curr) {
            echo "{$curr['ip']}:{$curr['port']}";
        }
        

        只要您的阵列完全平行,这将起作用。您可以考虑在 HTML 表单中添加数字索引,而不是依赖于排序。

        【讨论】:

        • 好的,阵列总是平行的,但为了可靠性,我还是考虑改变它。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        • 2013-11-18
        • 1970-01-01
        • 2014-11-29
        相关资源
        最近更新 更多