0

My site has lot of javascript, our frontend designer embedded settings code as json format, in js files . I need to this settings json value, from php server . and there are some dynamic changes. I coverted this settings json value into php array .. there are lot of them . so I written a function to convert into php code format

I have written a function toPhp, which is posted in http://jsfiddle.net/2HKMU/

I tried to convert one js file settings into php code .. which works ..

var config_topfive = {
    "type":"topfive", 
    "options":{ 
        "ascending":false, "percentage":true, "limits":[25,5] 
    }
};

console.log( toPhp( config_topfive ) );

which prints

array(
    "type" => "topfive", 
    "options" =>  array(
        "ascending" => false, 
        "percentage" => true, 
        "limits" => array(25,5)
    )
)

I would like to know, I miss anything on this conversion .. how could I improve this function more better ..

I know json_decode , but I would like to keep this setting json value in js file ,which made by designer in to php config file .. eg : config_topfive.php . I will copy paste console print of this function console.log( toPhp( config_topfive ) ) into php file

so final my php code in config_topfive.php look like

<?php

return array(
    "type" => "topfive", 
    "options" =>  array(
        "ascending" => false, 
        "percentage" => true, 
        "limits" => array(25,5)
    )
);
4

2 回答 2

3

为什么你不使用 json_decodejson_encode

http://www.php.net/manual/en/function.json-decode.php

http://www.php.net/manual/en/function.json-encode.php

样本 json_decode

 $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
 print_r(json_decode($json));

样本json_encode

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

 echo json_encode($arr);

输出

 {"a":1,"b":2,"c":3,"d":4,"e":5}

编辑:

我写了这个测试

function MakeArray(Json){
  this.JArray=Json;
  this.PArray=function(ar){
      for (var J in ar) {
            var Type=typeof(ar[J])
            if(Type=="string"){
                ar[J]="\""+ar[J]+"\"";
            }else if( Type== "object") {
                if(ar[J] instanceof Array){
                   ar[J]=this.PArray(ar[J]);
                   //ar[J]="\""+J+"\"=>array("+ar[J].join(',')+")";
                   ar[J]="array("+ar[J].join(',')+")";
                }else{
                   ar[J]="\""+J+"\"=>array("+this.MArray(ar[J])+")";
                }
                //ar[J]=this.MArray(ar[J]);       
            }
      }
      return ar;
  }
  this.MArray=function(IArray){
      var Output=Array();
      var Count=0;
      for (var i in IArray) {
          var Type=typeof(IArray[i]);
          if( Type== "object") {
              if(IArray[i] instanceof Array){
                  IArray[i]=this.PArray(IArray[i]);
                  Out="\""+i+"\"=>array("+IArray[i].join(',')+")";
              }else{
                  Out="\""+i+"\"=>array("+this.MArray(IArray[i])+")";
              }
          }else{
              if(Type=="string" || typeof(i)=="string"){
                  Out="\""+i+"\"=>\""+IArray[i]+"\"";
              }else if(Type=="number"){
                  Out=IArray[i];
              }else{
                  Out="\""+i+"\"=>"+IArray[i]; 
              }

          }
          Output[Count++]=Out;
      }
      return Output.join(',');

  }
  return "array("+this.MArray(this.JArray)+")";

}
document.getElementById('php').innerHTML =MakeArray(config_topfive);
于 2013-04-11T11:22:55.517 回答
1

json_decode将允许您获得相同的结果,并允许您传递一个“关联”布尔值来确定您希望它作为普通数组还是关联数组。

json_encode将允许您执行相反的操作。

于 2013-04-11T11:25:15.417 回答