0

I've spent hours trying to find the answer to this question, but I'm struggling. I'm reasonably familiar with PHP and the various in-built functions, and can build a complex foreach() loop to do this, but I thought I'd ask to see if anyone has a smarter solution to my problem. I have an array in this form, Now I want to convert this into multidimensional based on list_id matched on next instance.I tried this solution but not worked well https://stackoverflow.com/a/65042103/4626270. This may be duplicate but not got output here

<pre>Array
(
    [0] => Array
        (
            [list_id] => 5
            [order_list_name] => meri list 3
            [0] => Array
                (
                    [list_id] => 5
                    [product_id] => 1
                    [product_name] => DUCHESS WHITE WINE 200 ml
                    [sku] => SKU0001
                    [qty] => 2
                )

        )

    [1] => Array
        (
            [list_id] => 5
            [order_list_name] => meri list 3
            [0] => Array
                (
                    [list_id] => 5
                    [product_id] => 2
                    [product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
                    [sku] => SKU0002
                    [qty] => 8
                )

        )

    [2] => Array
        (
            [list_id] => 7
            [order_list_name] => meri list 9
            [0] => Array
                (
                    [list_id] => 7
                    [product_id] => 2
                    [product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
                    [sku] => SKU0002
                    [qty] => 2
                )

        )

)

I am expecting output like this

<pre>Array
    (
        [0] => Array
            (
                [list_id] => 5
                [order_list_name] => meri list 3
                [0] => Array
                    (
                        [list_id] => 5
                        [product_id] => 1
                        [product_name] => DUCHESS WHITE WINE 200 ml
                        [sku] => SKU0001
                        [qty] => 2
                    ),
                [1] => Array
                    (
                        [list_id] => 5
                        [product_id] => 1
                        [product_name] => DUCHESS WHITE WINE 200 ml
                        [sku] => SKU0001
                        [qty] => 2
                )
    
            )
        [1] => Array
            (
                [list_id] => 7
                [order_list_name] => meri list 9
                [0] => Array
                    (
                        [list_id] => 7
                        [product_id] => 2
                        [product_name] => CONNEXION DOUBLE ROCK 350 ml, Pack of 2
                        [sku] => SKU0002
                        [qty] => 2
                    )
    
            )
    
    )
4
  • Can you give us an input as a string? so we can easily copy it. Commented Feb 25, 2022 at 11:00
  • This is in my code $recordNew = array('list_id' => $record['list_id'], 'order_list_name' => $record['order_list_name'], ['list_id' => $record['list_id'], 'product_id' => $product->getId(), 'product_name' => $product->getName(), 'sku' => $product->getSku(), 'qty' => $record['qty']]); Commented Feb 25, 2022 at 11:05
  • Edit your question instead of post here in the comment, and provide us full example (so array fullfilled) Commented Feb 25, 2022 at 12:08
  • Please present your sample data as var_export() (preferred) or a json string so that volunteers can instantly use it in their preferred test environment. Commented Feb 25, 2022 at 12:40

1 Answer 1

1

Use temporary first level keys based on list_id to form groups. When finished, remove the temporary keys with array_values().

$grouped = [];
foreach ($records as $record) {
    if (!isset($grouped[$record['list_id']])) {
        $grouped[$record['list_id']] = [
            'list_id' => $record['list_id'],
            'order_list_name' => $record['order_list_name']
        ];
    }
    $grouped[$record['list_id']][] = [
        'list_id' => $record['list_id'],
        'product_id' => $product->getId(),
        'product_name' => $product->getName(),
        'sku' => $product->getSku(),
        'qty' => $record['qty']
    ];
}
var_export(
    array_values($grouped)
);

Removing the condition will also deliver the same result; it will just keep silently overwriting the two associative elements.

$grouped = [];
foreach ($records as $record) {
    $grouped[$record['list_id']]['list_id'] = $record['list_id'];
    $grouped[$record['list_id']]['order_list_name'] = $record['order_list_name'];
    $grouped[$record['list_id']][] = [
        'list_id' => $record['list_id'],
        'product_id' => $product->getId(),
        'product_name' => $product->getName(),
        'sku' => $product->getSku(),
        'qty' => $record['qty']
    ];
}
var_export(
    array_values($grouped)
);
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.