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

StackOverflow https://stackoverflow.com/questions/23471096

Pergunta

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.

Foi útil?

Solução

$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);
            }
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top