PHP陣列轉換為js陣列

牙小木木發表於2019-02-16

背景:

當我們用mvc分層到時候,有時候需要初始化一些變數,並assign 至view的模版中,其中有一些是smarty這種引擎解析的php變數,有時候也需要來一發js解析的陣列變數,以下例子就是簡單拼接。

程式碼

<?php
//原始城市php 陣列
$php_array=array (
  1 =>
  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 .= `);`."
";
                }
                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]

相關文章