如何从现有数组创建一个新的数组,使相同索引的所有数据将在该索引键下在一起


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的关联数组。这个数组本质上是动态的,也就是说它可以包含任何no。的元素。为了供您参考,我已经生成了只有两个元素的$_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);
            }
        }
    }
}