Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to Grouping array in php

I would like to know how to combine and overwrite the already existed array value in php.

my current array look like:

Array
(
    [1149] => 3
    [4108] => 5
)

As shown above values, i need expected result in php as below :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Array
(
    [0] => Array
        (
            [offer_id] => 1149
            [quantity] => 3
        )

    [1] => Array
        (
            [offer_id] => 4108
            [quantity] => 5
        )
)

>Solution :

How about:

$result = [];
foreach (
    [
        1149 => 3,
        4108 => 3,
    ] as $key => $value
) {
    $result[] = [
        'offer_id' => $key,
        'quantity' => $value,
    ];
}

print_r($result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading