编辑代码

<?php 
//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
// $arr = [
//     ['白色', '米色'],
//     ['牛皮', '猪皮'],
//     ['1cm,2cm'],
//     ['左', '右'],
//     ['#'],
//     ['#'],
//     ['#'],
// ];

// function combineArrays($arr, $temp = []) {
//     $result = [];
//     if (count($arr) == 0) {
//         $result[] = $temp;
//     } else {
//         $current = array_shift($arr);
//         foreach ($current as $value) {
//             $temp[] = $value;
//             $result = array_merge($result, combineArrays($arr, $temp));
//             array_pop($temp);
//         }
//     }
//     return $result;
// }

// $arrs = combineArrays($arr);
// print_r($arrs);
// $rate = 0.1;
// $order_body_title =  [
//     "颜色",
//     "材料",
//     [
//         "尺码",
//         [
//             "34",
//             "35",
//             "36",
//             "37",
//             "38",
//             "39"
//         ]
//     ],
//     "数量",
//     "单价",
//     "总价",
//     "含税价"
// ];

// $order_body_data = [
//     [
//         "白色",
//         "pu",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "白色",
//         "发泡",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "白色",
//         "橡胶",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "米色",
//         "pu",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "米色",
//         "发泡",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "米色",
//         "橡胶",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "红色",
//         "pu",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "红色",
//         "发泡",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ],
//     [
//         "红色",
//         "橡胶",
//         "34",
//         "35",
//         "36",
//         "37",
//         "38",
//         "39",
//         "#",
//         "#",
//         "#",
//         "#"
//     ]
// ];

// $data = [
//      [
//         'name'  => '米色,橡胶,34',
//         'num'   => 10,
//         'price' => 60
//     ],
//     [
//         'name'  => '米色,橡胶,36',
//         'num'   => 10,
//         'price' => 60
//     ],
//     [
//         'name'  => '米色,发泡,36',
//         'num'   => 10,
//         'price' => 60
//     ],
//     [
//         'name'  => '米色,发泡,37',
//         'num'   => 10,
//         'price' => 60
//     ],
// ];

// $result = [];

// foreach ($data as $item) {
//     $name_parts = explode(',', $item['name']);
//     $color = $name_parts[0];
//     $material = $name_parts[1];
//     $size = $name_parts[2];

//     $temp = [$color, $material];
//     foreach ($order_body_data as $row) {
//         if ($row[0] === $color && $row[1] === $material) {
//             for ($i = 2; $i < count($row); $i++) {
//                 if ($row[$i] === $size) {
//                     $temp[] = 0;
//                     $temp[] = 0;
//                     $temp[] = $item['num'];
//                     $temp[] = $item['num'];
//                     $temp[] = 0;
//                     $temp[] = 0;
//                     $temp[] = $item['num'] * $item['price'];
//                     $temp[] = $item['num'] * $item['price'] * (1 + $rate);
//                     $result[] = $temp;
//                     break;
//                 }
//             }
//         }
//     }
// }

// print_r($result);

function orderBodyData($rate, $order_body_title, $order_body_data, $data)
{
    $result = [];
    foreach ($data as $item) {
        $arr = explode(',', $item['name']);
        $color = $arr[0];
        $material = $arr[1];
        $size = $arr[2];
        $num = $item['num'];
        $price = $item['price'];
        $total_price = $price * $num;
        $tax_price = $total_price * (1 + $rate);
        $result[] = [
            $color,
            $material,
            0,
            0,
            $size,
            $size,
            0,
            0,
            $num,
            $num,
            $total_price,
            $tax_price
        ];
    }
    $result_temp = [];
    foreach ($result as $key => $value) {
        $material = $value[1];
        if (!isset($result_temp[$material])) {
            $result_temp[$material] = $value;
        } else {
            $result_temp[$material][3] += $value[3];
            $result_temp[$material][4] += $value[4];
            $result_temp[$material][8] += $value[8];
            $result_temp[$material][9] += $value[9];
            $result_temp[$material][10] += $value[10];
            $result_temp[$material][11] += $value[11];
        }
    }
    $result = array_values($result_temp);
    return $result;
}

$rate = 0.1;
$order_body_title = [
    "颜色",
    "材料",
    [
        "尺码",
        [
            "34",
            "35",
            "36",
            "37",
            "38",
            "39"
        ]
    ],
    "数量",
    "单价",
    "总价",
    "含税价"
];

$order_body_data = [
    [
        "白色",
        "pu",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "白色",
        "发泡",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "白色",
        "橡胶",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "米色",
        "pu",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "米色",
        "发泡",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "米色",
        "橡胶",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "红色",
        "pu",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "红色",
        "发泡",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ],
    [
        "红色",
        "橡胶",
        "34",
        "35",
        "36",
        "37",
        "38",
        "39",
        "#",
        "#",
        "#",
        "#"
    ]
];

$data = [
    [
        'name'  => '米色,发泡,36',
        'num'   => 10,
        'price' => 60
    ],
    [
        'name'  => '米色,发泡,37',
        'num'   => 10,
        'price' => 60
    ],
    [
        'name'  => '米色,橡胶,36',
        'num'   => 10,
        'price' => 60
    ],
    [
        'name'  => '米色,橡胶,34',
        'num'   => 10,
        'price' => 60
    ],
];

$result = orderBodyData($rate, $order_body_title, $order_body_data, $data);
var_dump($result);