Home > Mobile >  Create multidimensional array based on value
Create multidimensional array based on value

Time:02-26

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
                    )
    
            )
    
    )

CodePudding user response:

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)
);
  • Related