背景:
当我们用mvc分层到时候,有时候需要初始化一些变量,并assign 至view的模版中,其中有一些是smarty这种引擎解析的php变量,有时候也需要来一发js解析的数组变量,以下例子就是简单拼接。
代码
array ( 0 => '安徽-ah', 101 => '合肥-hf', 102 => '安庆-aq', 103 => '蚌埠-bb', 104 => '巢湖-ch', 105 => '池州-cz', 106 => '阜阳-fy', 107 => '淮北-hb', 108 => '淮南-hn', 109 => '六安-la', 110 => '马鞍山-mas', 112 => '宁国-ng', 113 => '铜陵-tl', 114 => '芜湖-wh', 115 => '宣城-xc', 116 => '滁州-cz', 117 => '亳州-bz', 118 => '黄山-hs', 120 => '宿州-sz', 121 => '桐城-tc', ), 2 => array ( 0 => '北京-bj', 201 => '北京-bj', ), 3 => array ( 0 => '福建-fj', 301 => '福州-fz', 302 => '厦门-xm', 303 => '龙岩-ly', 305 => '漳州-zz', 306 => '莆田-pt', 307 => '泉州-qz', 309 => '武夷山-wys', 314 => '南平-np', 315 => '宁德-nd', 318 => '三明-sm', ), 4 => array ( 0 => '甘肃-gs', 401 => '兰州-lz', 402 => '定西-dx', 405 => '平凉-pl', 407 => '临夏-lx', 409 => '酒泉-jq', 410 => '张掖-zy', 411 => '庆阳-qy', 412 => '武威-ww', 415 => '天水-ts', 416 => '白银-by', 3113 => '金昌-jc', ), 5 => array ( 0 => '广东-gd', 501 => '广州-gz', 502 => '深圳-sz', 503 => '珠海-zh', 504 => '东莞-dg', 505 => '中山-zs', 507 => '汕头-st', 510 => '潮州-cz', 511 => '韶关-sg', 513 => '湛江-zj', 514 => '肇庆-zq', 515 => '茂名-mm', 516 => '梅州-mz', 518 => '佛山-fs', 520 => '惠州-hz', 521 => '江门-jm', 522 => '揭阳-jy', 524 => '清远-qy', 528 => '云浮-yf', 532 => '阳江-yj', 535 => '河源-hy', 550 => '汕尾-sw', 1012 => '揭东-jd', 8110 => '顺德-sd', ), 6 => array ( 0 => '广西-gx', 601 => '南宁-nn', 602 => '柳州-lz', 603 => '桂林-gl', 604 => '北海-bh', 605 => '百色-bs', 608 => '贵港-gg', 610 => '玉林-yl', 612 => '钦州-qz', 613 => '梧州-wz', 618 => '防城港-fcg', ), 7 => array ( 0 => '贵州-gz', 701 => '贵阳-gy', 702 => '遵义-zy', 703 => '兴义-xy', 705 => '安顺-as', 706 => '毕节-bj', 707 => '都匀-dy', 708 => '六盘水-lps', 709 => '凯里-kl', 710 => '铜仁-tr', ), 8 => array ( 0 => '海南-hn', 801 => '海口-hk', 803 => '三亚-sy', 8101 => '儋州-dz', ), 9 => array ( 0 => '河北-hb', 901 => '石家庄-sjz', 902 => '唐山-ts', 903 => '邢台-xt', 905 => '秦皇岛-qhd', 906 => '廊坊-lf', 907 => '邯郸-hd', 908 => '衡水-hs', 909 => '沧州-cz', 910 => '保定-bd', 911 => '张家口-zjk', 912 => '承德-cd', 3112 => '三河-sh', ), 10 => array ( 0 => '河南-hn', 1001 => '郑州-zz', 1002 => '洛阳-ly', 1004 => '信阳-xy', 1005 => '新乡-xx', 1006 => '商丘-sq', 1007 => '三门峡-smx', 1008 => '濮阳-py', 1009 => '南阳-ny', 1011 => '焦作-jz', 1013 => '开封-kf', 1014 => '安阳-ay', 1016 => '鹤壁-hb', 1018 => '平顶山-pds', 1019 => '济源-jy', 1021 => '驻马店-zmd', ), 21 => array ( 0 => '山东-sd', 1015 => '德州-dz', 2101 => '济南-jn', 2102 => '青岛-qd', 2103 => '烟台-yt', 2104 => '威海-wh', 2105 => '潍坊-wf', 2106 => '泰安-ta', 2107 => '枣庄-zz', 2109 => '淄博-zb', 2110 => '东营-dy', 2111 => '高密-gm', 2112 => '菏泽-hz', 2113 => '滨州-bz', 2114 => '聊城-lc', 2117 => '临沂-ly', 2118 => '济宁-jn', 2120 => '日照-rz', ), 11 => array ( 0 => '黑龙江-hlj', 1101 => '哈尔滨-heb', 1102 => '大庆-dq', 1103 => '齐齐哈尔-qqhe', 1104 => '鹤岗-hg', 1106 => '佳木斯-jms', 1107 => '鸡西-jx', 1108 => '牡丹江-mdj', 1112 => '伊春-yc', 1116 => '绥化-sh', 1118 => '双鸭山-sys', )); function get_provice_city_js($arrData=[],$jsVarName=''){ $js_result=''; $intCount=0; foreach($arrData as $intProv => $arrAreaData) { $js_result .= $jsVarName.'['.$intCount++.'] = new Array(\''.$intProv.'-'.$arrAreaData[0].'\''; //再循环省下面的城市 foreach($arrAreaData as $intCity => $strCity) { //如果是省份,忽略 if ($intCity == 0) { continue; } $js_result .= ',\''.$intCity.'-'.$strCity.'\''; } $js_result .= ');'."\n"; } return $js_result;}echo get_provice_city_js($php_array,'myProject.');
输出
myProject.[0] = new Array('1-安徽-ah','101-合肥-hf','102-安庆-aq','103-蚌埠-bb','104-巢湖-ch','105-池州-cz','106-阜阳-fy','107-淮北-hb','108-淮南-hn','109-六安-la','110-马鞍山-mas','112-宁国-ng','113-铜陵-tl','114-芜湖-wh','115-宣城-xc','116-滁州-cz','117-亳州-bz','118-黄山-hs','120-宿州-sz','121-桐城-tc');myProject.[1] = new Array('2-北京-bj','201-北京-bj');myProject.[2] = new Array('3-福建-fj','301-福州-fz','302-厦门-xm','303-龙岩-ly','305-漳州-zz','306-莆田-pt','307-泉州-qz','309-武夷山-wys','314-南平-np','315-宁德-nd','318-三明-sm');myProject.[3] = new Array('4-甘肃-gs','401-兰州-lz','402-定西-dx','405-平凉-pl','407-临夏-lx','409-酒泉-jq','410-张掖-zy','411-庆阳-qy','412-武威-ww','415-天水-ts','416-白银-by','3113-金昌-jc');myProject.[4] = new Array('5-广东-gd','501-广州-gz','502-深圳-sz','503-珠海-zh','504-东莞-dg','505-中山-zs','507-汕头-st','510-潮州-cz','511-韶关-sg','513-湛江-zj','514-肇庆-zq','515-茂名-mm','516-梅州-mz','518-佛山-fs','520-惠州-hz','521-江门-jm','522-揭阳-jy','524-清远-qy','528-云浮-yf','532-阳江-yj','535-河源-hy','550-汕尾-sw','1012-揭东-jd','8110-顺德-sd');myProject.[5] = new Array('6-广西-gx','601-南宁-nn','602-柳州-lz','603-桂林-gl','604-北海-bh','605-百色-bs','608-贵港-gg','610-玉林-yl','612-钦州-qz','613-梧州-wz','618-防城港-fcg');myProject.[6] = new Array('7-贵州-gz','701-贵阳-gy','702-遵义-zy','703-兴义-xy','705-安顺-as','706-毕节-bj','707-都匀-dy','708-六盘水-lps','709-凯里-kl','710-铜仁-tr');myProject.[7] = new Array('8-海南-hn','801-海口-hk','803-三亚-sy','8101-儋州-dz');myProject.[8] = new Array('9-河北-hb','901-石家庄-sjz','902-唐山-ts','903-邢台-xt','905-秦皇岛-qhd','906-廊坊-lf','907-邯郸-hd','908-衡水-hs','909-沧州-cz','910-保定-bd','911-张家口-zjk','912-承德-cd','3112-三河-sh');myProject.[9] = new Array('10-河南-hn','1001-郑州-zz','1002-洛阳-ly','1004-信阳-xy','1005-新乡-xx','1006-商丘-sq','1007-三门峡-smx','1008-濮阳-py','1009-南阳-ny','1011-焦作-jz','1013-开封-kf','1014-安阳-ay','1016-鹤壁-hb','1018-平顶山-pds','1019-济源-jy','1021-驻马店-zmd');myProject.[10] = new Array('21-山东-sd','1015-德州-dz','2101-济南-jn','2102-青岛-qd','2103-烟台-yt','2104-威海-wh','2105-潍坊-wf','2106-泰安-ta','2107-枣庄-zz','2109-淄博-zb','2110-东营-dy','2111-高密-gm','2112-菏泽-hz','2113-滨州-bz','2114-聊城-lc','2117-临沂-ly','2118-济宁-jn','2120-日照-rz');myProject.[11] = new Array('11-黑龙江-hlj','1101-哈尔滨-heb','1102-大庆-dq','1103-齐齐哈尔-qqhe','1104-鹤岗-hg','1106-佳木斯-jms','1107-鸡西-jx','1108-牡丹江-mdj','1112-伊春-yc','1116-绥化-sh','1118-双鸭山-sys');[Finished in 0.1s]