簡體   English   中英

如何從現有數組創建新數組,以使相同索引的所有數據都在該索引鍵下?

[英]How to create a new array from an existing array such that all data of same index will be together under that index key?

我有一個名為$_POST的關聯數組。 該數組本質上是動態的,可以包含任何數字。 的元素。 供您參考,我僅使用兩個元素生成了$_POST數組。 該數組的內容如下:

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

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

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

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

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

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

    [rebate_start_date] => Array
        (
            [1] => 2014-05-01
            [2] => 2014-06-01
        )

    [rebate_expiry_date] => Array
        (
            [1] => 2014-05-31
            [2] => 2014-06-30
        )

    [applicable_states] => Array
        (
            [1] => Array
                (
                    [0] => 3
                    [1] => 4
                    [2] => 10
                    [3] => 15
                    [4] => 17
                )

            [2] => Array
                (
                    [0] => 44
                    [1] => 45
                    [2] => 47
                    [3] => 49
                    [4] => 50
                )

        )

    [multiselect] => 50
    [rebate_total_count] => Array
        (
            [1] => 5000
            [2] => 10000
        )

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

)

現在,我想要一個新數組,以使相同索引的所有數據都應位於值等於該索引的鍵下。 如果您對我的要求感到困惑,請查看以下要生成的數組:

Array
  (
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46    
    [1] => Array 
        (
        [pack] => 10
        [quantity] => 20
        [volume] => 30
        [units] => 7
        [amount] => 40
        [rebate_start_date] => 2014-05-01
        [rebate_expiry_date] => 2014-05-31
        [rebate_total_count] => 5000
        [applicable_states] => Array 
                (
                 [0] => 3
                 [1] => 4
                 [2] => 10
                 [3] => 15
                 [4] => 17
                )   
        [product_id_1] => Array
                (
                    [1] => 10
                    [2] => 9
                )
        )

    [2] => Array
      (
        [pack] => 50
        [quantity] => 60
        [volume] => 70  
        [units] => 12
        [amount] => 80    
        [rebate_start_date] => 2014-06-01
        [rebate_expiry_date] => 2014-06-30
        [rebate_total_count] => 10000
        [applicable_states] => Array 
          (
            [0] => 44
            [1] => 45
            [2] => 47
            [3] => 49
            [4] => 50
          )
        [product_id_2] => Array
          (
            [1] => 11
            [2] => 8
          )
      )

      [multiselect] => 50
)

在上面的數組中,您可以看到匹配索引的所有數據都在同一鍵下。 我想告訴您一件事,鍵[applicable_states][rebate_total_count]可能包含空白值。 在操作數組$_POST時也應考慮到這一點,以便生成包含一個索引的所有具有相同索引的數據的數組,如上所述。 如何通過使用現成的數組函數和PHP中的一些神奇邏輯以最佳方式實現這一目標? 謝謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM