简体   繁体   中英

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:

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

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

How about:

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

print_r($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM