新人php数组问题

新人php数组问题0 goods_common是现在我的数组格式,怎么写和goods_base格式一样。十万火急啊,先谢谢各位大神帮忙

回复讨论(解决方案)

你没有给出原始数据

$goods_base = array();
$arr = array();
$str = ‘i_’;
foreach($goods_common as $v){
$arr[key($v)] = current($v);
$str .= key($v);
}
$goods_base[$str] = $arr;
print_r($goods_base);

$goods_common = array(
‘1’=>array(
’22’=>”粉红”,
’23’=>”黑色”
),
‘2’=>array(
’24’=>’75a’,
’25’=>’75b’,
’26’=>’75c’
)
);
print_r($goods_common);
$arritem = array();
foreach($goods_common as $k=>$item){
$k_new =”;
foreach($item as $k2=>$v){
$k_new.= $k2;
}
$arritem[$k.’_’.$k_new] =$item;
}
print_r($arritem);

$goods_base = array();
$arr = array();
$str = ‘i_’;
foreach($goods_common as $v){
$arr[key($v)] = current($v);
$str .= key($v);
}
$goods_base[$str] = $arr;
print_r($goods_base);

谢谢了,格式已经对了,顺便问一下 我goods_spec遍历的时候循环goods_base[2]里面的值,怎么把goods_base[2]里面的值组合成goods_common这样的数组 最终拿到的数据应该和goods_common这个数组一样
$goods_spec = array(
“1”=>array(
“22”=> “颜色随机”,
),
“2”=>array(
“24”=> “75a”,
“25”=> “75b”,
“26”=> “75c”
)
)
$common_spec = array(
0 =>array(
“i_2224″=>array(
“22”=> “颜色随机”,
“24”=> “75a”,
)
),
1 =>array(
“i_2225″=>array(
“22”=> “颜色随机”,
“25”=> “75b”,
)
),
2 =>array(
“i_2226″=>array(
“22”=> “颜色随机”,
“26”=> “75c”
)
)
)

$goods_base = array();
$arr = array();
$str = ‘i_’;
foreach($goods_common as $v){
$arr[key($v)] = current($v);
$str .= key($v);
}
$goods_base[$str] = $arr;
print_r($goods_base);

谢谢了,格式已经对了,顺便问一下 我goods_spec遍历的时候循环goods_base[2]里面的值,怎么把goods_base[2]里面的值组合成$common_spec这样的数组 最终拿到的数据应该和$common_spec这个数组一样
$goods_spec = array(
“1”=>array(
“22”=> “颜色随机”,
),
“2”=>array(
“24”=> “75a”,
“25”=> “75b”,
“26”=> “75c”
)
)
$common_spec = array(
0 =>array(
“i_2224″=>array(
“22”=> “颜色随机”,
“24”=> “75a”,
)
),
1 =>array(
“i_2225″=>array(
“22”=> “颜色随机”,
“25”=> “75b”,
)
),
2 =>array(
“i_2226″=>array(
“22”=> “颜色随机”,
“26”=> “75c”
)
)
)

上面那个goods_common写错了,应该是common_spec

goods_base[2]是什么?你直接重新发下你的原始数据,和期望结果吧。

goods_base[2]是什么?你直接重新发下你的原始数据,和期望结果吧。

原始数据是这个
$goods_spec = array(
“1”=>array(
“22”=> “颜色随机”,
),
“2”=>array(
“24”=> “75a”,
“25”=> “75b”,
“26”=> “75c”
)
)

最终的结果应该是这个
$common_spec = array(
0 =>array(
“i_2224″=>array(
“22”=> “颜色随机”,
“24”=> “75a”,
)
),
1 =>array(
“i_2225″=>array(
“22”=> “颜色随机”,
“25”=> “75b”,
)
),
2 =>array(
“i_2226″=>array(
“22”=> “颜色随机”,
“26”=> “75c”
)
)
)

$goods_spec = array(
“1”=>array(
“22”=> “颜色随机”,
),
“2”=>array(
“24”=> “75a”,
“25”=> “75b”,
“26”=> “75c”
)
);
$common_spec = array();
foreach($goods_spec[2] as $k=>$v){
$arr = array();
$str = ‘i_’;
$str .= key($goods_spec[1]).$k;
$arr[key($goods_spec[1])] = current($goods_spec[1]);
$arr[$k] = $v;
$common_spec[][$str] = $arr;
}
print_r( $common_spec);

array
(
[0] => array
(
[i_2224] => array
(
[22] => 颜色随机
[24] => 75a
)
)
[1] => array
(
[i_2225] => array
(
[22] => 颜色随机
[25] => 75b
)
)
[2] => array
(
[i_2226] => array
(
[22] => 颜色随机
[26] => 75c
)
)
)

$goods_spec = array(
“1”=>array(
“22”=> “颜色随机”,
),
“2”=>array(
“24”=> “75a”,
“25”=> “75b”,
“26”=> “75c”
)
);
$common_spec = array();
foreach($goods_spec[2] as $k=>$v){
$arr = array();
$str = ‘i_’;
$str .= key($goods_spec[1]).$k;
$arr[key($goods_spec[1])] = current($goods_spec[1]);
$arr[$k] = $v;
$common_spec[][$str] = $arr;
}
print_r( $common_spec);

array
(
[0] => array
(
[i_2224] => array
(
[22] => 颜色随机
[24] => 75a
)
)
[1] => array
(
[i_2225] => array
(
[22] => 颜色随机
[25] => 75b
)
)
[2] => array
(
[i_2226] => array
(
[22] => 颜色随机
[26] => 75c
)
)
)

已解决,谢谢了朋友

Posted in 未分类