在跟踪数组操作时我做错了什么?

时间:2014-05-12 13:57:52

标签: php arrays multidimensional-array associative-array

我的数组$_POST如下:

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46
    [product_id_1] => Array
        (
            [1] => 9
            [2] => 11
        )

    [pack] => Array
        (
            [1] => 10
            [2] => 50
        )

    [quantity] => Array
        (
            [1] => 20
            [2] => 60
        )

    [volume] => Array
        (
            [1] => 30
            [2] => 70
        )

    [units] => Array
        (
            [1] => 12
            [2] => 7
        )

    [amount] => Array
        (
            [1] => 40
            [2] => 80
        )

    [product_id_2] => Array
        (
            [1] => 10
            [2] => 8
        )

    [rebate_start_date] => 2014-05-28
    [rebate_expiry_date] => 2014-05-31
    [applicable_states] => Array
        (
            [0] => 2
            [1] => 9
            [2] => 16
            [3] => 18
        )

    [multiselect] => 18
    [rebate_total_count] => 8000
)

我正在操作上面的数组以保持数据具有相同的索引,即索引1的数据应该在一个数组中,索引2的数据应该在另一个数组中,依此类推......它也在工作但是适用的州正在受到干扰。适用的状态很常见。它们不属于任何索引。怎么避免这个?

$rebate_by_product = array();
      foreach ($_POST as $key => $val) {
        if (!is_array($val)) {
          $rebate_by_product[$key] = $val;
        } elseif (preg_match('/^product_id_(\d+)$/', $key, $match)) {
          $i = $match[1];
          if (isset($rebate_by_product[$i])) {
            $rebate_by_product[$i][$key] = $val;
          } else {
            $rebate_by_product[$i] = array($key => $val);
          }
        } else {
            foreach ($val as $i => $subval) {
              if (isset($rebate_by_product[$i])) {
                $rebate_by_product[$i][$key] = $subval;
              } else {
                $rebate_by_product[$i] = array($key => $subval);
              }
            }
          }
      }

经过上述操作后,如果我打印数组,则如下:

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46
    [1] => Array
        (
            [product_id_1] => Array
                (
                    [1] => 9
                    [2] => 11
                )

            [pack] => 10
            [quantity] => 20
            [volume] => 30
            [units] => 12
            [amount] => 40
            [applicable_states] => 9
        )

    [2] => Array
        (
            [pack] => 50
            [quantity] => 60
            [volume] => 70
            [units] => 7
            [amount] => 80
            [product_id_2] => Array
                (
                    [1] => 10
                    [2] => 8
                )

            [applicable_states] => 16
        )

    [rebate_start_date] => 2014-05-28
    [rebate_expiry_date] => 2014-05-31
    [0] => Array
        (
            [applicable_states] => 2
        )

    [3] => Array
        (
            [applicable_states] => 18
        )

    [multiselect] => 18
    [rebate_total_count] => 8000
)

您可以从上面的数组中观察到适用状态数组是否受到干扰。我想避免这种干扰。你能否纠正我在阵列操作中犯的错误?感谢。

2 个答案:

答案 0 :(得分:1)

显式测试所需的密钥并将其分配给输出数组。

$rebate_by_product = array();
      foreach ($_POST as $key => $val) {
        if (!is_array($val)) {
          $rebate_by_product[$key] = $val;
        } elseif ($key == 'applicable_states') {
           $rebate_by_product[$key] = $val;
        } elseif (preg_match('/^product_id_(\d+)$/', $key, $match)) {
          $i = $match[1];
          if (isset($rebate_by_product[$i])) {
            $rebate_by_product[$i][$key] = $val;
          } else {
            $rebate_by_product[$i] = array($key => $val);
          }
        } else {
            foreach ($val as $i => $subval) {
              if (isset($rebate_by_product[$i])) {
                $rebate_by_product[$i][$key] = $subval;
              } else {
                $rebate_by_product[$i] = array($key => $subval);
              }
            }
          }
      }

答案 1 :(得分:0)

applicable_states为零索引,因此键不会按预期方式对齐。如果您可以控制表单输入,则可以使用PHP本机数组转换,以便$ _POST数组按预期方式进入。例如,如果您有一个带

的HTML表单
<input type="text" name="product[1][name]" value="something">
<input type="text" name="product[1][price]" value="15">
<input type="number" name="product[1][quantity]" value="30">
<input type="text" name="product[2][name]" value="something else">
<input type="text" name="product[2][price]" value="5">
<input type="number" name="product[2][quantity]" value="65">

当PHP在$ _POST中看到名称时,它会自动将它们变成嵌套数组。请参阅此处的第一条评论http://www.php.net/manual/en/reserved.variables.post.php