当前位置:Gxlcms > PHP教程 > 请教这个一维数组如何根据键名相关性转化成二维数组?多谢

请教这个一维数组如何根据键名相关性转化成二维数组?多谢

时间:2021-07-01 10:21:17 帮助过:7人阅读

请问这个一维数组怎么根据键名相关性转化成二维数组?谢谢
谢谢各位大哥!我的一维数组如下
PHP code
?Array ( [xm1] => 人工新砌管道井 [dj1] => 300 [sl1] => 100 [zj1] => 30000 [xm2] => 卫生间回填 [dj2] => 100 [sl2] => [zj2] => [xm3] => 电视背景墙 [dj3] => 300 [sl3] => [zj3] => [xm4] => 客厅酒柜电视背景酒柜 [dj4] => 100 [sl4] => [zj4] => [xm5] => 入户门鞋柜装饰(活动送鞋柜) [dj5] => 130 [sl5] => [zj5] => [xm6] => 书柜隔断(含静音棉夹层) [dj6] => 200 [sl6] => [zj6] => [xm8] => 厨房移动门头储藏柜 [dj8] => 200 [sl8] => [zj8] => [xm9] => 儿童房衣柜隔断(含静音棉夹层) [dj9] => 100 [sl9] => [zj9] => [xm10] => 厨房橱柜 [dj10] => 200 [sl10] => [zj10] => [xm11] => 卫生间洗手台 [dj11] => 300 [sl11] => [zj11] => [xm12] => 客厅、餐厅、过道、石膏板造型吊顶 [dj12] => 350 [sl12] => [zj12] => [xm13] => 卫生间、厨房集成吊顶 [dj13] => 250 [sl13] => [zj13] => [xm14] => 顶、墙面双飞粉乳胶漆 [dj14] => 200 [sl14] => [zj14] => [xm15] => 厨房、洗衣房、卫生间防滑地砖 [dj15] => 120 [sl15] => [zj15] => [xm16] => 厨房、卫生间墙面镶贴300*450墙砖 [dj16] => 200 [sl16] => [zj16] => [xm17] => 窗台石铺设 [dj17] => 200 [sl17] => [zj17] => [xm18] => 门槛石 [dj18] => 400 [sl18] => [zj18] => [xm19] => 防盗窗 [dj19] => 400 [sl19] => [zj19] => [xm20] => 书房门套 [dj20] => 30 [sl20] => [zj20] => [xm21] => 卫生间洗衣房门套 [dj21] => 60 [sl21] => [zj21] => [xm22] => 地、墙面防水、防潮处理 [dj22] => 70 [sl22] => [zj22] => [xm23] => 水、电路改造 [dj23] => 400 [sl23] => [zj23] => [xm24] => 完工保洁服务 [dj24] => 150 [sl24] => [zj24] => [xm25] => 成品保护费 [dj25] => 200 [sl25] => [zj25] => [xm26] => 材料搬运费 [dj26] => 5 [sl26] => [zj26] => [xm27] => 垃圾清运费 [dj27] => 5 [sl27] => [zj27] => [xm28] => 厨房地砖(活动7折优惠) [dj28] => 120 [sl28] => [zj28] => [xm29] => 厨房墙砖(活动7折优惠) [dj29] => 140 [sl29] => [zj29] => [xm30] => 卫生间地砖(活动7折优惠) [dj30] => 100 [sl30] => [zj30] => [xm31] => 卫生间墙砖(活动7折优惠) [dj31] => 80 [sl31] => [zj31] => [xm32] => 强化地板(活动买一送一) [dj32] => 170 [sl32] => [zj32] => [xm33] => 卧室套装门 [dj33] => 730 [sl33] => [zj33] => [xm34] => 卫生间门 [dj34] => 570 [sl34] => [zj34] => [xm35] => 厨房移动门 [dj35] => 300 [sl35] => [zj35] => [xm36] => 衣柜移动门 [dj36] => 300 [sl36] => [zj36] => [xm37] => 厨房、卫生间集吊顶 [dj37] => 260 [sl37] => [zj37] => [xm38] => 窗台石、橱柜台面石 [dj38] => 260 [sl38] => [zj38] => [xm39] => 门槛石 [dj39] => 140 [sl39] => [zj39] => ) 


我想转化成如下
PHP code

Array (
[0]=>Array(
[xm] => 人工新砌管道井
[dj] => 300 
[sl] => 100 
[zj] => 30000
[1]=>Array(
......
)
)
)



------解决方案--------------------
PHP code

$res    = array();
foreach($arr AS $k => $v)
{
    $res[substr($k, 2)-1][substr($k, 0, 2)]    = $v;
}
/*

Array
(
    [0] => Array
        (
            [xm] => 人工新砌管道井
            [dj] => 300
            [sl] => 100
            [zj] => 30000
        )

    [1] => Array
        (
            [xm] => 卫生间回填
            [dj] => 100
            [sl] => 
            [zj] => 
        )
*/                     

人气教程排行