I've array $_POST as follows:

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
)

I'm manipulating above array in order to keep data with equal index together i.e. data of index 1 should be in one array, data of index 2 should be in another array, and so on... It's working also but the applicaable states are getting disturbed. Applicable states are common. They don't belong to any index. How to avoid this?

$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);
              }
            }
          }
      }

After above manipulation if I print the array it's as follows:

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
)

You can observe from above array that the array of applicable states is disturbed. I want to avoid that disturbance. Can you please correct the mistake I'm making in array manipulation? Thanks.

有帮助吗?

解决方案

explicitly test for the required key and assign it to the output array.

$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);
              }
            }
          }
      }

其他提示

applicable_states is zero indexed so the keys aren't going to align as expected. If you can control the form input, you could use PHPs native array conversion so that your $_POST array comes in as expected. For example, if you have an HTML form with

<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">

When PHP sees the names in $_POST, it will automatically turn them into a nested array. See the first comment here http://www.php.net/manual/en/reserved.variables.post.php

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top