- 今天遇到了这么个需求,其实要的数据格式就和笛卡尔积类似,如下 :
$userids = [1,2];
$classids = ['4','5','6'];
$datas = [
['userid' => 1,'classid' => '4'],
['userid' => 1,'classid' => '5'],
['userid' => 1,'classid' => '6'],
['userid' => 2,'classid' => '4'],
['userid' => 2,'classid' => '5'],
['userid' => 2,'classid' => '6'],
];
$userids = [1,2];
$classids = ['4','5','6'];
$index = 0;
$data = [];
foreach($userids as $userid) {
foreach($classids as $classid) {
$data[$index]['userid'] = $userid;
$data[$index]['classid'] = $classid;
$index ++;
}
}
$userids = [1,2];
$classids = ['4','5','6'];
foreach($userids as $userid) {
foreach($classids as $classid) {
$data[] = [
'userid' => $userid,
'classid' => $classid
];
}
}