资讯专栏INFORMATION COLUMN

PHP数组转换为js数组

cyrils / 1693人阅读

摘要:背景当我们用分层到时候,有时候需要初始化一些变量,并至的模版中,其中有一些是这种引擎解析的变量,有时候也需要来一发解析的数组变量,以下例子就是简单拼接。

背景:

当我们用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 .= ");"."
";
                }
                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]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/30675.html

相关文章

  • PHP数组转换js数组

    摘要:背景当我们用分层到时候,有时候需要初始化一些变量,并至的模版中,其中有一些是这种引擎解析的变量,有时候也需要来一发解析的数组变量,以下例子就是简单拼接。 背景: 当我们用mvc分层到时候,有时候需要初始化一些变量,并assign 至view的模版中,其中有一些是smarty这种引擎解析的php变量,有时候也需要来一发js解析的数组变量,以下例子就是简单拼接。 代码

    dingding199389 评论0 收藏0
  • PHPJS数组转换

    因为程序用到,之前又没写过,费了很大的劲才写出来。 主要是编辑文章部分,读取,修改关键词 发布文章部分 //定义JS的数组 var tagslist = []; //然后把用户输入的标签添加到这个数组里 tagslist.push(测试1); //发送到后台的时候进行了编码,ECMAScript v3 反对使用escape,所以尽量不用了 encodeURI(tagslist) //后台接...

    scq000 评论0 收藏0
  • PHPJS数组转换

    因为程序用到,之前又没写过,费了很大的劲才写出来。 主要是编辑文章部分,读取,修改关键词 发布文章部分 //定义JS的数组 var tagslist = []; //然后把用户输入的标签添加到这个数组里 tagslist.push(测试1); //发送到后台的时候进行了编码,ECMAScript v3 反对使用escape,所以尽量不用了 encodeURI(tagslist) //后台接...

    kycool 评论0 收藏0
  • 前端知识点总结——AJAX

    摘要:对象的语法对象名属性值属性值属性值属性值林黛玉这个对象所对应的属性如下林黛玉如果对象取值的时候,直接对象属性名称就可以林黛玉什么是对象表现方式对象表示法,即以对象的格式表现出来的字符串。 前端知识点总结——Ajax 1.ajax 1.URL的作用 用于表示任意一个资源的位置(互联网上) 2.详解 格式: ://:@:/;?# scheme:方案协议,以哪种方式到服务获...

    hzc 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<