新人求教数组问题

$goods_spec_cond = array(
1=>array(
“2”=> “蓝色”,
“3”=> “红色”,
“21”=> “颜色随机”,
“23”=> “黑色”,
“27”=> “灰色”,
“31”=> “白色”
),
2=>array(
“4”=> “s”,
“5”=> “m”,
“6”=> “l”,
“28”=> “均码”,
“32”=> “xl”,
“33”=> “xxl”
)
) 这样的一个数组 如何遍历 让一个颜色对应下面多个全部尺码,最终结果应该是下面这样
array(
“i_24″=>array(
“2”=> “蓝色”,
“4”=> “s”
),
“i_25″=>array(
“2”=> “蓝色”,
“5”=> “m”
),
“i_26″=>array(
“2”=> “蓝色”,
“6”=> “l”
),
“i_228″=>array(
“2”=> “蓝色”,
“28”=> “均码”,
),
“i_232″=>array(
“2”=> “蓝色”,
“32”=> “xl”,
),
“i_233″=>array(
“2”=> “蓝色”,
“33”=> “xxl”,
),
“i_34″=>array(
“3”=> “红色”,
“4”=> “s”,
),
)数组没写完,但是格式是这个样子的 i_24 是 键名和键值组合起来的,请各位大神帮帮忙,方便的话给思路最好

回复讨论(解决方案)

$goods_spec_cond = array(
1=>array(
“2”=> “蓝色”,
“3”=> “红色”,
“21”=> “颜色随机”,
“23”=> “黑色”,
“27”=> “灰色”,
“31”=> “白色”
),
2=>array(
“4”=> “s”,
“5”=> “m”,
“6”=> “l”,
“28”=> “均码”,
“32”=> “xl”,
“33”=> “xxl”
)
);
foreach($goods_spec_cond[1] as $k1=>$v1)
foreach($goods_spec_cond[2] as $k2=>$v2)
$res[“i_$k1$k2”] = array($k1 => $v1, $k2 => $v2);
var_export($res);

array (
‘i_24’ =>
array (
2 => ‘蓝色’,
4 => ‘s’,
),
‘i_25’ =>
array (
2 => ‘蓝色’,
5 => ‘m’,
),
‘i_26’ =>
array (
2 => ‘蓝色’,
6 => ‘l’,
),
‘i_228’ =>
array (
2 => ‘蓝色’,
28 => ‘均码’,
),
‘i_232’ =>
array (
2 => ‘蓝色’,
32 => ‘xl’,
),
‘i_233’ =>
array (
2 => ‘蓝色’,
33 => ‘xxl’,
),
‘i_34’ =>
array (
3 => ‘红色’,
4 => ‘s’,
),
‘i_35’ =>
array (
3 => ‘红色’,
5 => ‘m’,
),
‘i_36’ =>
array (
3 => ‘红色’,
6 => ‘l’,
),
‘i_328’ =>
array (
3 => ‘红色’,
28 => ‘均码’,
),
‘i_332’ =>
array (
3 => ‘红色’,
32 => ‘xl’,
),
‘i_333’ =>
array (
3 => ‘红色’,
33 => ‘xxl’,
),
‘i_214’ =>
array (
21 => ‘颜色随机’,
4 => ‘s’,
),
‘i_215’ =>
array (
21 => ‘颜色随机’,
5 => ‘m’,
),
‘i_216’ =>
array (
21 => ‘颜色随机’,
6 => ‘l’,
),
‘i_2128’ =>
array (
21 => ‘颜色随机’,
28 => ‘均码’,
),
‘i_2132’ =>
array (
21 => ‘颜色随机’,
32 => ‘xl’,
),
‘i_2133’ =>
array (
21 => ‘颜色随机’,
33 => ‘xxl’,
),
‘i_234’ =>
array (
23 => ‘黑色’,
4 => ‘s’,
),
‘i_235’ =>
array (
23 => ‘黑色’,
5 => ‘m’,
),
‘i_236’ =>
array (
23 => ‘黑色’,
6 => ‘l’,
),
‘i_2328’ =>
array (
23 => ‘黑色’,
28 => ‘均码’,
),
‘i_2332’ =>
array (
23 => ‘黑色’,
32 => ‘xl’,
),
‘i_2333’ =>
array (
23 => ‘黑色’,
33 => ‘xxl’,
),
‘i_274’ =>
array (
27 => ‘灰色’,
4 => ‘s’,
),
‘i_275’ =>
array (
27 => ‘灰色’,
5 => ‘m’,
),
‘i_276’ =>
array (
27 => ‘灰色’,
6 => ‘l’,
),
‘i_2728’ =>
array (
27 => ‘灰色’,
28 => ‘均码’,
),
‘i_2732’ =>
array (
27 => ‘灰色’,
32 => ‘xl’,
),
‘i_2733’ =>
array (
27 => ‘灰色’,
33 => ‘xxl’,
),
‘i_314’ =>
array (
31 => ‘白色’,
4 => ‘s’,
),
‘i_315’ =>
array (
31 => ‘白色’,
5 => ‘m’,
),
‘i_316’ =>
array (
31 => ‘白色’,
6 => ‘l’,
),
‘i_3128’ =>
array (
31 => ‘白色’,
28 => ‘均码’,
),
‘i_3132’ =>
array (
31 => ‘白色’,
32 => ‘xl’,
),
‘i_3133’ =>
array (
31 => ‘白色’,
33 => ‘xxl’,
),
)

Posted in 未分类

发表评论