求商品规则笛卡尔积算法

镜面王子
  • 69

题目描述


$arr = [
    [
        "title"=>"颜色",
        "value"=>["黑色", "白色"]
    ],
    [
        "title"=>"尺码",
        "value"=>["S", "M","L"]
    ],
    [
        "title"=>"长度",
        "value"=>["9分裤", "长裤"]
    ]
];

如何才能让title标题加入分类中,每行一个规则

你期待的结果是什么?实际看到的错误信息又是什么?

我期待的结果是

array(12) {
  [0]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "S"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [1]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "S"
      ["长度"]=>
      string(6) "长裤"
    }
  }
  [2]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "M"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [3]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "M"
      ["长度"]=>
      string(6) "长裤"
    }
  }
  [4]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "L"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [5]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "黑色"
      ["尺码"]=>
      string(1) "L"
      ["长度"]=>
      string(6) "长裤"
    }
  }
  [6]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "S"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [7]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "S"
      ["长度"]=>
      string(6) "长裤"
    }
  }
  [8]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "M"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [9]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "M"
      ["长度"]=>
      string(6) "长裤"
    }
  }
  [10]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "L"
      ["长度"]=>
      string(7) "9分裤"
    }
  }
  [11]=>
  array(1) {
    ["value"]=>
    array(3) {
      ["颜色"]=>
      string(6) "白色"
      ["尺码"]=>
      string(1) "L"
      ["长度"]=>
      string(6) "长裤"
    }
  }
}
回复
阅读 1.1k
2 个回答
function func($sets)
{
    if (!$sets) return [];
    $first = array_shift($sets);
    $count = count($sets);
    for ($i = 0; $i < $count; $i++) {
        $tmp = [];
        if($i){
            foreach ($first as $f) {
                foreach ($sets[$i]['value'] as $set) {
                    $f[$sets[$i]['title']] = $set;
                    $tmp[] = $f;
                }
            }
        }else{
            foreach ($first['value'] as $f) {
                foreach ($sets[$i]['value'] as $set) {
                    $tmp[] = [
                        $first['title'] => $f,
                        $sets[$i]['title']=> $set
                    ];
                }
            }
        }
        $first = $tmp;
    }
    return $first;
}

$arr1 = [
    [
        "title"=>"颜色",
        "value"=>["黑色", "白色"]
    ],
    [
        "title"=>"尺码",
        "value"=>["S", "M","L"]
    ],
    [
        "title"=>"长度",
        "value"=>["9分裤", "长裤"]
    ],
    [
        "title"=>"性别",
        "value"=>["男", "女"]
    ]
];
$c = func($arr1);
print_r($c);
Array
(
    [0] => Array
        (
            [颜色] => 黑色
            [尺码] => S
            [长度] => 9分裤
            [性别] => 男
        )

    [1] => Array
        (
            [颜色] => 黑色
            [尺码] => S
            [长度] => 9分裤
            [性别] => 女
        )

    [2] => Array
        (
            [颜色] => 黑色
            [尺码] => S
            [长度] => 长裤
            [性别] => 男
        )

    [3] => Array
        (
            [颜色] => 黑色
            [尺码] => S
            [长度] => 长裤
            [性别] => 女
        )

    [4] => Array
        (
            [颜色] => 黑色
            [尺码] => M
            [长度] => 9分裤
            [性别] => 男
        )

    [5] => Array
        (
            [颜色] => 黑色
            [尺码] => M
            [长度] => 9分裤
            [性别] => 女
        )

    [6] => Array
        (
            [颜色] => 黑色
            [尺码] => M
            [长度] => 长裤
            [性别] => 男
        )

.......

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