1

Following is the input array:

$input = [
    [
        'id' => 96,
        'shipping_no' => 212755-1,
        'part_no' => 'reterty',
        'description' => 'tyrfyt',
        'packaging_type' => 'PC'
    ],
    [
        'id' => 96,
        'shipping_no' => 212755-1,
        'part_no' => 'dftgtryh',
        'description' => 'dfhgfyh',
        'packaging_type' => 'PC'
    ],
    [
        'id' => 97,
        'shipping_no' => 212755-2,
        'part_no' => 'ZeoDark',
        'description' => 's%c%s%c%s',
        'packaging_type' => 'PC'
    ]
];

I want the above to be transformed into like this:

$output = [
    [
        'key' => 96, 
        'value' => [
            [
                'shipping_no' => 212755-1, 
                'part_no' => 'reterty', 
                'description' => 'tyrfyt', 
                'packaging_type' => 'PC'
            ], 
            [
                'shipping_no' => 212755-1, 
                'part_no' => 'dftgtryh', 
                'description' => 'dfhgfyh', 
                'packaging_type' => 'PC'
            ]
        ]
    ],
    [
        'key' => 97, 
        'value' => [
            [
                'shipping_no' => 212755-2, 
                'part_no' => 'ZeoDark', 
                'description' => 's%c%s%c%s', 
                'packaging_type' => 'PC'
            ]
        ]
    ]
];

I have tried to implement it like this:

$result = [];
foreach ($input as $value) {
    $result[] = ['key' => $value['id'], 'value' => ['shipping_no' => $value['shipping_no'], 'part_no' => $value['part_no'], 'description' => $value['description'], 'packaging_type' => $value['packaging_type']]];
}

It is not getting grouped based on common key. Please help me with the possible approach that I should take to solve this.

0

1 Answer 1

1

I can see that you've done a good job of crafting the new subarray structure, but the grouping should come first and because of how the grouping is done with temporary keys, the restructuring code can be simplified.

Code: (Demo)

$result = [];
foreach ($input as $row) {
    $id = $row['id'];
    unset($row['id']);
    if (isset($result[$id])) {
        $result[$id]['value'][] = $row;
    } else {
        $result[$id] = [
            'key' => $id,
            'value' => [$row]
        ];
    }
}
var_export(
    array_values($result)
);

To explain the process:

  1. As you iterate the input array, cache the id of each encountered row of data.
  2. Because you do not wish to retain the $row['id'] in your value subarray, you can now safely remove that element from the $row.
  3. Then check if there is an existing group for the current $id with isset($result[$id]).
  4. If the group already exists, then you can merely push the $row data as a new indexed row into the group's value subarray.
  5. If the group does not already exist, then it needs to have all of the desired structure declared/populated. This means that the key element is declared and the value subarray must be declared with its first entry.
  6. Finally, if you don't want the first level keys remove them with array_values().
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.