PHP 求帮助来了,我需要把 源数据转换成 结果数据,求各位大佬指教

孟玄德
  • 23

PHP 求帮助来了,我需要把 源数据转换成 结果数据,求各位大佬指教

规则是:当weight 相加 <= 3000 的 时候

源数据:

[
    {
        "order_id": 11213131343242,
        "proince": "广东省",
        "city": "广州市",
        "area": "天河区",
        "detailAddress": "华南农业大学",
        "detail": [
            {
                "order_id": 11213131343242,
                "s_order_id": 111111112,
                "num": 1,
                "coding": 266601,
                "weight": 480
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111113,
                "num": 1,
                "coding": 266602,
                "weight": 1500
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111114,
                "num": 1,
                "coding": 266603,
                "weight": 1500
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111115,
                "num": 1,
                "coding": 266604,
                "weight": 840
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111116,
                "num": 1,
                "coding": 266605,
                "weight": 750
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111117,
                "num": 1,
                "coding": 266606,
                "weight": 260
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111118,
                "num": 1,
                "coding": 266607,
                "weight": 750
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111119,
                "num": 1,
                "coding": 266608,
                "weight": 100
            },
            {
                "order_id": 11213131343242,
                "s_order_id": 111111121,
                "num": 1,
                "coding": 266609,
                "weight": 230
            }
        ]
    }
]

结果数据:

[{
    "order_id": 11213131343242,
    "proince": "广东省",
    "city": "广州市",
    "area": "天河区",
    "detailAddress": "华南农业大学",
    "detail": [
        {
            "order_id": 11213131343242,
            "s_order_id": 111111112,
            "num": 1,
            "coding": 266601,
            "weight": 480
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111113,
            "num": 1,
            "coding": 266602,
            "weight": 1500
        }
    ]
},{
    "order_id": 11213131343242,
    "proince": "广东省",
    "city": "广州市",
    "area": "天河区",
    "detailAddress": "华南农业大学",
    "detail": [
        {
            "order_id": 11213131343242,
            "s_order_id": 111111114,
            "num": 1,
            "coding": 266603,
            "weight": 1500
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111115,
            "num": 1,
            "coding": 266604,
            "weight": 840
        }
    ]
},{
    "order_id": 11213131343242,
    "proince": "广东省",
    "city": "广州市",
    "area": "天河区",
    "detailAddress": "华南农业大学",
    "detail": [
        {
            "order_id": 11213131343242,
            "s_order_id": 111111116,
            "num": 1,
            "coding": 266605,
            "weight": 750
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111117,
            "num": 1,
            "coding": 266606,
            "weight": 260
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111118,
            "num": 1,
            "coding": 266607,
            "weight": 750
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111119,
            "num": 1,
            "coding": 266608,
            "weight": 100
        },
        {
            "order_id": 11213131343242,
            "s_order_id": 111111121,
            "num": 1,
            "coding": 266609,
            "weight": 230
        }
    ]
}]
回复
阅读 439
1 个回答

看起来是这样。

$head = null;
$result = [];
foreach ($arr as $group) {
    $detail = $group['detail'];
    $group['detail'] = [];
    $head = $group;
    unset($group);
    $groups = [];
    foreach ($detail as $list) {
        reset($groups);
        $last = array_pop($groups);
        if (empty($last)) {
            $groups[] = [$list];
            continue;
        }

        $sum = array_sum(array_column($last, 'weight'));
        if ($sum + $list['weight'] > 3000) {
            $groups[] = $last;
            $groups[] = [$list];
        } else {
            $last[] = $list;
            $groups[] = $last;
        }
    }

    foreach ($groups as $g2) {
        $result[] = array_merge($head, [
            'detail' => $g2,
        ]);
    }
}

die(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏