0

I have a script which gets orders from a mysql database and returns a multidimensional array called orders which is formatted as below when i do a print_r($orders).

As you can see the ordered items are grouped to each order as an array with some item details.

Array
(
    [0] => Array
        (
            [order_id] => 2646
            [0] => Array
                (
                    [code] => 2811
                    [qty] => 1
                    [weight] => 6500
                )

            [1] => Array
                (
                    [code] => 2812A
                    [qty] => 3
                    [weight] => 10000
                )

        )

    [1] => Array
        (
            [order_id] => 2647
            [0] => Array
                (
                    [code] => 2812A
                    [qty] => 2
                    [weight] => 10000
                )

            [1] => Array
                (
                    [code] => 2810
                    [qty] => 2
                    [weight] => 10800
                )

        )

    [2] => Array
        (
            [order_id] => 2650
            [0] => Array
                (
                    [code] => 2906
                    [qty] => 1
                    [weight] => 12700
                )

            [1] => Array
                (
                    [code] => 2908
                    [qty] => 6
                    [weight] => 11900
                )

            [2] => Array
                (
                    [code] => 2909
                    [qty] => 3
                    [weight] => 11400
                )

            [3] => Array
                (
                    [code] => 2913
                    [qty] => 1
                    [weight] => 11000
                )

        )

    [3] => Array
        (
            [order_id] => 2658
            [0] => Array
                (
                    [code] => 2880
                    [qty] => 3
                    [weight] => 10900
                )

            [1] => Array
                (
                    [code] => 2881
                    [qty] => 3
                    [weight] => 8100
                )

            [2] => Array
                (
                    [code] => 2882
                    [qty] => 1
                    [weight] => 12700
                )

        )

)

What I am trying to do is rework the $orders array so it appears like what I have below that is the ordered items are merged into the parent array and the index for those items starts counting at 1.

Array
(
    [0] => Array
        (
            [order_id] => 2646
            [code_1] => 2811
            [qty_1] => 1
            [weight_1] => 6500
            [code_2] => 2812A
            [qty_2] => 3
            [weight_2] => 10000
        )

    [1] => Array
        (
            [order_id] => 2647
            [code_1] => 2812A
            [qty_1] => 2
            [weight_1] => 10000
            [code_2] => 2810
            [qty_2] => 2
            [weight_2] => 10800
        )

    [2] => Array
        (
            [order_id] => 2650
            [code_1] => 2906
            [qty_1] => 1
            [weight_1] => 12700
            [code_2] => 2908
            [qty_2] => 6
            [weight_2] => 11900
            [code_3] => 2909
            [qty_3] => 3
            [weight_3] => 11400
            [code_4] => 2913
            [qty_4] => 1
            [weight_4] => 11000
        )

    [3] => Array
        (
            [order_id] => 2658
            [code_1] => 2880
            [qty_1] => 3
            [weight_1] => 10900
            [code_2] => 2881
            [qty_2] => 3
            [weight_2] => 8100
            [code_3] => 2882
            [qty_3] => 1
            [weight_3] => 12700
        )

)

I have tried so many snippets of code and functions that I am either confused or totally off track. Any feedback or advice is greatly appreciated.

jh314
  • 27,144
  • 16
  • 62
  • 82
Jamie
  • 31
  • 5

0 Answers0