2014-05-12 82 views
0

我已經陣列$_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 
) 

您可以從上面的數組,適用的狀態數組被幹擾觀察。我想避免這種干擾。你能糾正我在數組操作中犯的錯誤嗎?謝謝。

回答

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

applicable_states是零索引,所以鍵不會按預期對齊。如果您可以控制表單輸入,則可以使用PHP本地數組轉換,以便您的$ _POST數組按照預期進入。例如,如果您有

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

HTML表單當PHP看到在$ _ POST的名字,它會自動把它們變成一個嵌套數組。請參閱此處的第一條評論http://www.php.net/manual/en/reserved.variables.post.php