dqg95034 2014-05-12 13:57
浏览 16
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dsi37923 2014-05-12 14:45
    关注

    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);
                  }
                }
              }
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码