Skip to content

Instantly share code, notes, and snippets.

@imbyc
Last active July 21, 2023 11:50
Show Gist options
  • Save imbyc/553f314545a7d90de7daf00b5785f715 to your computer and use it in GitHub Desktop.
Save imbyc/553f314545a7d90de7daf00b5785f715 to your computer and use it in GitHub Desktop.
[修正sku列表顺序] #笛卡尔积

skulist的顺序和根据sale_props生成的笛卡尔积顺序不一致

需要一个函数修改正skulist顺序 让他和 sale_props生成的笛卡尔积顺序 一致

$json = '{"sale_props":[{"Id":"","Text":"颜色","Values":[{"Id":"","Text":"红色","pic":"https://static.leitool.com/shequntui/admin_up/103407_821521_12788.png","alias":"大红色"},{"Id":"","Text":"蓝色","pic":"https://static.leitool.com/shequntui/admin_up/103413_181761_36723.png","alias":"天蓝色"}]},{"Id":"","Text":"尺码","Values":[{"Id":"","Text":"X"},{"Id":"","Text":"M"}]}],"skulist":[{"id":"1619155904428830724","item_id":"1689932510544667110","sku_id":"1689906988110003683","attribute":"蓝色;M","props":"颜色:蓝色;尺码:M"},{"id":"1619155904428830727","item_id":"1689932510544667110","attribute":"红色;M","props":"颜色:红色;尺码:M"},{"id":"1619155904428830728","item_id":"1689932510544667110","sku_id":"1689906988110003660","attribute":"红色;X","props":"颜色:红色;尺码:X"},{"id":"1619155904428830726","item_id":"1689932510544667110","sku_id":"1689906988110003681","attribute":"蓝色;X","props":"颜色:蓝色;尺码:X"}]}';

$arr = json_decode($json, true);
$sale_props = $arr['sale_props'];
$skulist    = $arr['skulist'];

$skulist = sortSkulist($sale_props, $skulist);

$arr['skulist'] = $skulist;

$json = json_encode($arr, JSON_UNESCAPED_UNICODE);

echo $json;

结果:

{
    "sale_props": [
        {
            "Id": "",
            "Text": "颜色",
            "Values": [
                {
                    "Id": "",
                    "Text": "红色",
                    "pic": "https://static.leitool.com/shequntui/admin_up/103407_821521_12788.png",
                    "alias": "大红色"
                },
                {
                    "Id": "",
                    "Text": "蓝色",
                    "pic": "https://static.leitool.com/shequntui/admin_up/103413_181761_36723.png",
                    "alias": "天蓝色"
                }
            ]
        },
        {
            "Id": "",
            "Text": "尺码",
            "Values": [
                {
                    "Id": "",
                    "Text": "X"
                },
                {
                    "Id": "",
                    "Text": "M"
                }
            ]
        }
    ],
    "skulist": [
        {
            "id": "1619155904428830728",
            "item_id": "1689932510544667110",
            "sku_id": "1689906988110003660",
            "attribute": "红色;X",
            "props": "颜色:红色;尺码:X"
        },
        {
            "id": "1619155904428830727",
            "item_id": "1689932510544667110",
            "attribute": "红色;M",
            "props": "颜色:红色;尺码:M"
        },
        {
            "id": "1619155904428830726",
            "item_id": "1689932510544667110",
            "sku_id": "1689906988110003681",
            "attribute": "蓝色;X",
            "props": "颜色:蓝色;尺码:X"
        },
        {
            "id": "1619155904428830724",
            "item_id": "1689932510544667110",
            "sku_id": "1689906988110003683",
            "attribute": "蓝色;M",
            "props": "颜色:蓝色;尺码:M"
        }
    ]
}
{
"sale_props": [
{
"Id": "",
"Text": "颜色",
"Values": [
{
"Id": "",
"Text": "红色",
"pic": "https://static.leitool.com/shequntui/admin_up/103407_821521_12788.png",
"alias": "大红色"
},
{
"Id": "",
"Text": "蓝色",
"pic": "https://static.leitool.com/shequntui/admin_up/103413_181761_36723.png",
"alias": "天蓝色"
}
]
},
{
"Id": "",
"Text": "尺码",
"Values": [
{
"Id": "",
"Text": "X"
},
{
"Id": "",
"Text": "M"
}
]
}
],
"skulist": [
{
"id": "1619155904428830724",
"item_id": "1689932510544667110",
"sku_id": "1689906988110003683",
"attribute": "蓝色;M",
"props": "颜色:蓝色;尺码:M"
},
{
"id": "1619155904428830727",
"item_id": "1689932510544667110",
"attribute": "红色;M",
"props": "颜色:红色;尺码:M"
},
{
"id": "1619155904428830728",
"item_id": "1689932510544667110",
"sku_id": "1689906988110003660",
"attribute": "红色;X",
"props": "颜色:红色;尺码:X"
},
{
"id": "1619155904428830726",
"item_id": "1689932510544667110",
"sku_id": "1689906988110003681",
"attribute": "蓝色;X",
"props": "颜色:蓝色;尺码:X"
}
]
}
<?php
function sortSkulist($sale_props, $skulist)
{
// 生成属性组合的笛卡尔积
$cartesian = [[]];
foreach ( $sale_props as $prop ) {
$temp = [];
$attribute = $prop['Text']; // 获取属性名,如“颜色”
$attributeValues = $prop['Values']; // 获取属性值列表,如["红色", "蓝色"]
foreach ( $cartesian as $cart ) {
foreach ( $attributeValues as $value ) {
$valueText = $value['Text']; // 获取属性值的文本,如"红色"
$temp[] = array_merge($cart, [$attribute => $valueText]); // 将属性值与已有的组合合并,形成新的组合
}
}
$cartesian = $temp; // 更新笛卡尔积数组
}
// 创建笛卡尔积组合与原始顺序之间的映射
$cartesianMap = [];
foreach ( $cartesian as $index => $cart ) {
$cartesianMap[implode(';', $cart)] = $index; // 使用组合标识作为键,原始顺序作为值,创建映射
}
// 根据笛卡尔积的顺序对 skulist 进行排序
usort($skulist, function ($a, $b) use ($cartesianMap) {
return $cartesianMap[$a['attribute']] - $cartesianMap[$b['attribute']]; // 根据笛卡尔积的映射值进行排序
});
return $skulist; // 返回排序后的 skulist
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment