如何创建从现有数组的新阵列,同样索引的所有数据将在一起指标项下? [英] How to create a new array from an existing array such that all data of same index will be together under that index key?

查看:129
本文介绍了如何创建从现有数组的新阵列,同样索引的所有数据将在一起指标项下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为关联数组 $ _ POST 。此数组在本质上也就是它可以包含任何没有动态。的元素。供您参考我已经生成只有两个元素 $ _ POST 阵列。该数组的内容如下:

 阵列

    [OP => preVIEW
    [ID] =>
    [form_submitted] =>是
    [COMPANY_ID] => 46
    [product_id_1] =>排列
        (
            [1] => 10
            [2] => 9
        )    [包装] =>排列
        (
            [1] => 10
            [2] => 50
        )    [数量] =>排列
        (
            [1] => 20
            [2] => 60
        )    [体积] =>排列
        (
            [1] =>三十
            [2] => 70
        )    [单位] =>排列
        (
            [1] => 7
            [2] => 12
        )    [金额] =>排列
        (
            [1] => 40
            [2] => 80
        )    [rebate_start_date] =>排列
        (
            [1] => 2014-05-01
            [2] => 2014-06-01
        )    [rebate_expiry_date] =>排列
        (
            [1] => 2014年5月31日
            [2] => 2014-06-30
        )    [applicable_states] =>排列
        (
            [1] =>排列
                (
                    [0] => 3
                    [1] => 4
                    [2] => 10
                    [3] => 15
                    [4] => 17
                )            [2] =>排列
                (
                    [0] => 44
                    [1] => 45
                    [2] => 47
                    [3] => 49
                    [4] => 50
                )        )    [多选] => 50
    [rebate_total_count] =>排列
        (
            [1] => 5000
            [2] => 10000
        )    [product_id_2] =>排列
        (
            [1] => 11
            [2] => 8
        ))

现在我想在这样一种方式,同一索引的全部数据应该是其值等于该索引密钥下一个新的数组。如果你跟我要求看看下面的数组,我想产生混淆:

 阵列
  (
    [OP => preVIEW
    [ID] =>
    [form_submitted] =>是
    [COMPANY_ID] => 46
    [1] =>排列
        (
        [包装] => 10
        [数量] => 20
        [体积] =>三十
        [单位] => 7
        [金额] => 40
        [rebate_start_date] => 2014-05-01
        [rebate_expiry_date] => 2014年5月31日
        [rebate_total_count] => 5000
        [applicable_states] =>排列
                (
                 [0] => 3
                 [1] => 4
                 [2] => 10
                 [3] => 15
                 [4] => 17
                )
        [product_id_1] =>排列
                (
                    [1] => 10
                    [2] => 9
                )
        )    [2] =>排列
      (
        [包装] => 50
        [数量] => 60
        [体积] => 70
        [单位] => 12
        [金额] => 80
        [rebate_start_date] => 2014-06-01
        [rebate_expiry_date] => 2014-06-30
        [rebate_total_count] => 10000
        [applicable_states] =>排列
          (
            [0] => 44
            [1] => 45
            [2] => 47
            [3] => 49
            [4] => 50
          )
        [product_id_2] =>排列
          (
            [1] => 11
            [2] => 8
          )
      )      [多选] => 50

在上面的阵列,你可以观察到匹配索引的所有数据是根据相同的密钥。还有一件事我想告诉你,键 [applicable_states] [rebate_total_count] 可能包含空值。这件事也应同时操纵阵列被认为 $ _ POST 为了产生含有相同​​索引的所有数据在一个键如上的阵列。如何通过使用现成的阵列功能和PHP一些神奇的逻辑来实现这一点的最佳方法是什么?谢谢。


解决方案

  $ new_array =阵列();
的foreach($ _ POST为$关键=> $ VAL){
    如果(!is_array($ VAL)){
        $ new_array [$关键] = $ VAL;
    } ELSEIF(preg_match('/ ^ _ PRODUCT_ID(\\ D +)$ /',$键,$比赛)){
        $ I = $匹配[1];
        如果(使用isset($ new_array [$ i])){
            $ new_array [$ i] [$关键] = $ VAL;
        }其他{
            $ new_array [$ i] =阵列($关键=> $ VAL);
        }
    }其他{
        的foreach($ VAL为$ I => $ subval){
            如果(使用isset($ new_array [$ i])){
                $ new_array [$ i] [$关键] = $ subval;
            }其他{
                $ new_array [$ i] =阵列($关键=> $ subval);
            }
        }
    }
}

I've an associative array titled $_POST. This array is dynamic in nature that is it can contain any no. of elements. For your reference I've generated the $_POST array with only two elements. The content of this array is as follows:

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
        )

)

Now I want a new array in such a way that all the data of same index should be under a key whose value is equal to that index. If you get confused with my requirement take a look at the following array which I want to generate:

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
)

In above array you can observe that all the data of matching index is under same key. One more thing I want to tell you that the keys [applicable_states] and [rebate_total_count] may contain blank values. This thing also should be considered while manipulating the array $_POST in order to generate the array containing all data of same index under one key as above. How to achieve this in optimum way by using ready made array functions and some magical logic in PHP? Thank You.

解决方案

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

这篇关于如何创建从现有数组的新阵列,同样索引的所有数据将在一起指标项下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆