二维数组排序组合 [ 新手入门 ]
$temp = array(
'尺码' => array("1", "2" , "3"),
"颜色" => array("红", "黄", "蓝"),
"形状" => array("A", "B", "C"),
);
function spanToArray($array) {
static $i = 0;$i++;
$len = count($array);
$keys = array_keys($array);
if ($len >= 2) {
$key1 = array_shift($keys);
$key2 = array_shift($keys);
$arr1 = $array[$key1];
$arr2 = $array[$key2];
$len1 = count($arr1);
$len2 = count($arr2);
$temp = array();
foreach ($arr1 as $k1 => $v1) {
foreach ($arr2 as $k2 => $v2) {
if (is_array($v1)) {
$v1[$key2] = $v2;
$temp[] = $v1;
} else {
$temp[] = array($key1 => $v1, $key2 => $v2);
}
}
}
$newArray = array();
$newArray[] = $temp;
foreach ($keys as $value) {
$newArray[$value] = $array[$value];
}
return doExchange($newArray);
} else {
$tmpArr = array();
$arr = $array[$keys[0]];
foreach ($arr as $k => $v) {
if ($i > 1) {
$tmpArr[] = $v;
} else {
$tmpArr[][$keys[0]] = $v;
}
}
return $tmpArr;
}
}
print_r(doExchange($temp));
执行结果
Array
(
[0] => Array
(
[尺码] => 1
[颜色] => 红
[形状] => A
)
[1] => Array
(
[尺码] => 1
[颜色] => 红
[形状] => B
)
[2] => Array
(
[尺码] => 1
[颜色] => 红
[形状] => C
)
[3] => Array
(
[尺码] => 1
[颜色] => 黄
[形状] => A
)
[4] => Array
(
[尺码] => 1
[颜色] => 黄
[形状] => B
)
[5] => Array
(
[尺码] => 1
[颜色] => 黄
[形状] => C
)
[6] => Array
(
[尺码] => 1
[颜色] => 蓝
[形状] => A
)
[7] => Array
(
[尺码] => 1
[颜色] => 蓝
[形状] => B
)
[8] => Array
(
[尺码] => 1
[颜色] => 蓝
[形状] => C
)
[9] => Array
(
[尺码] => 2
[颜色] => 红
[形状] => A
)
[10] => Array
(
[尺码] => 2
[颜色] => 红
[形状] => B
)
[11] => Array
(
[尺码] => 2
[颜色] => 红
[形状] => C
)
[12] => Array
(
[尺码] => 2
[颜色] => 黄
[形状] => A
)
[13] => Array
(
[尺码] => 2
[颜色] => 黄
[形状] => B
)
[14] => Array
(
[尺码] => 2
[颜色] => 黄
[形状] => C
)
[15] => Array
(
[尺码] => 2
[颜色] => 蓝
[形状] => A
)
[16] => Array
(
[尺码] => 2
[颜色] => 蓝
[形状] => B
)
[17] => Array
(
[尺码] => 2
[颜色] => 蓝
[形状] => C
)
[18] => Array
(
[尺码] => 3
[颜色] => 红
[形状] => A
)
[19] => Array
(
[尺码] => 3
[颜色] => 红
[形状] => B
)
[20] => Array
(
[尺码] => 3
[颜色] => 红
[形状] => C
)
[21] => Array
(
[尺码] => 3
[颜色] => 黄
[形状] => A
)
[22] => Array
(
[尺码] => 3
[颜色] => 黄
[形状] => B
)
[23] => Array
(
[尺码] => 3
[颜色] => 黄
[形状] => C
)
[24] => Array
(
[尺码] => 3
[颜色] => 蓝
[形状] => A
)
[25] => Array
(
[尺码] => 3
[颜色] => 蓝
[形状] => B
)
[26] => Array
(
[尺码] => 3
[颜色] => 蓝
[形状] => C
)
)
共 0 条回复
没有找到数据。
flysolo 河南郑州
注册时间:2011-07-07
最后登录:2018-01-03
在线时长:10小时25分
最后登录:2018-01-03
在线时长:10小时25分
- 粉丝11
- 金钱540
- 威望20
- 积分840