原数组如下:Array([0] => 商城A[1] => 测试项目1[2] => 1000[3] => 1个样品[4] => 3[5] => 商城B[6] => 测试项目2[7] => 2000[8] => 2个样品[9] => 4[10] => 商城C[11] => 测试项目3[12] => 1000[13] => 3个样品[14] => 5[15] => 商城D[16] =>测试项目4[17] => 1000[18] => 4个样品[19] => 6[20] => 商城E[21] => 测试项目5[22] => 1000[23] => 5个样品[24] => 7)转化为如下:Array([data] => Array([0] => Array([title] => 商城A[price] => 1000[project] => 测试项目1[sum] => 1个样品[cycle] => 3)[1] => Array([title] => 商城B[price] => 2000[project] => 测试项目2[sum] => 2个样品[cycle] => 4)[2] => Array([title] =>商城C[price] => 1000[project] => 测试项目3[sum] => 3个样品[cycle] => 5)[3] => Array([title] =>商城D[price] => 1000[project] =>测试项目4[sum] => 4个样品[cycle] => 6)[4] => Array([title] => 商城E[price] => 1000[project] => 测试项目5[sum] => 4个样品[cycle] => 7)))求解答
2 回答
aluckdog
TA贡献1847条经验 获得超7个赞
<?php $arr = Array( '0' => ' 商城A' , '1' => ' 测试项目1' , '2' => ' 1000' , '3' => ' 1个样品' , '4' => ' 3' , '5' => ' 商城B' , '6' => ' 测试项目2' , '7' => ' 2000' , '8' => ' 2个样品' , '9' => ' 4' , '10' => ' 商城C' , '11' => ' 测试项目3' , '12' => ' 1000' , '13' => ' 3个样品' , '14' => ' 5' , '15' => ' 商城D' , '16' => '测试项目4' , '17' => ' 1000' , '18' => ' 4个样品' , '19' => ' 6' , '20' => ' 商城E' , '21' => ' 测试项目5' , '22' => ' 1000' , '23' => ' 5个样品' , '24' => ' 7' , ); $arr = array_chunk ( $arr , 5); var_export( $arr ); $new = array (); foreach ( $arr as $key => $value ) { $newVal = array (); foreach ( $value as $k => $val ) { if ( $k == 0) { $newVal [ 'title' ] = $val ; } if ( $k == 1) { $newVal [ 'price' ] = $val ; } if ( $k == 2) { $newVal [ 'project' ] = $val ; } if ( $k == 3) { $newVal [ 'sum' ] = $val ; } if ( $k == 4) { $newVal [ 'cycle' ] = $val ; } } $new [ 'data' ][] = $newVal ; } var_export( $new ); die ; |
输出结果
array ( 0 => array ( 0 => ' 商城A' , 1 => ' 测试项目1' , 2 => ' 1000' , 3 => ' 1个样品' , 4 => ' 3' , ), 1 => array ( 0 => ' 商城B' , 1 => ' 测试项目2' , 2 => ' 2000' , 3 => ' 2个样品' , 4 => ' 4' , ), 2 => array ( 0 => ' 商城C' , 1 => ' 测试项目3' , 2 => ' 1000' , 3 => ' 3个样品' , 4 => ' 5' , ), 3 => array ( 0 => ' 商城D' , 1 => '测试项目4' , 2 => ' 1000' , 3 => ' 4个样品' , 4 => ' 6' , ), 4 => array ( 0 => ' 商城E' , 1 => ' 测试项目5' , 2 => ' 1000' , 3 => ' 5个样品' , 4 => ' 7' , ), ) 处理之后输出: array ( 'data' => array ( 0 => array ( 'title' => ' 商城A' , 'price' => ' 测试项目1' , 'project' => ' 1000' , 'sum' => ' 1个样品' , 'cycle' => ' 3' , ), 1 => array ( 'title' => ' 商城B' , 'price' => ' 测试项目2' , 'project' => ' 2000' , 'sum' => ' 2个样品' , 'cycle' => ' 4' , ), 2 => array ( 'title' => ' 商城C' , 'price' => ' 测试项目3' , 'project' => ' 1000' , 'sum' => ' 3个样品' , 'cycle' => ' 5' , ), 3 => array ( 'title' => ' 商城D' , 'price' => '测试项目4' , 'project' => ' 1000' , 'sum' => ' 4个样品' , 'cycle' => ' 6' , ), 4 => array ( 'title' => ' 商城E' , 'price' => ' 测试项目5' , 'project' => ' 1000' , 'sum' => ' 5个样品' , 'cycle' => ' 7' , ), ), ) |
catspeake
TA贡献1111条经验 获得超0个赞
$arr=Array
(
'0' => '商城A',
'1' => '测试项目1',
'2' => '1000',
'3' => '1个样品',
'4' => '3',
'5' => '商城B',
'6' => '测试项目2',
'7' => '2000',
'8' => '2个样品',
'9' => '4',
'10' => '商城C',
'11' => '测试项目3',
'12' => '1000',
'13' => '3个样品',
'14' => '5',
'15' => '商城D',
'16' => '测试项目4',
'17' => '1000',
'18' => '4个样品',
'19' => '6',
'20' => '商城E',
'21' => '测试项目5',
'22' => '1000',
'23' => '5个样品',
'24' => '7'
);
for($i=0;$i<5;$i++){
$st=$i*5;
$key=($i+1)*5;
foreach($arr as $k=>$v){
if($k>=$st && $k<$key){
$tem['data'][$i][]=$v;
}
}
}
- 2 回答
- 0 关注
- 128 浏览
添加回答
举报
0/150
提交
取消