189 8069 5689

javascript与php类型转换类

一、javascript:

  1. 代码:
    /**
    * 类型转换类
    * @author [lee] <[]>
    */
    function translation(data){
    this.data = new Array()
    this.info = new Array()
    this.init = function(){
        this.data = data
    }
    this.todo = function(){
        data = this.data
        info = this.info
        for(var i in info){
            for(var j in data){
                for(var k in data[j]){
                    if(i==k){
                        if(info[i]=='bool'){
                            data[j][k] = Boolean(data[j][k])
                        }else if(info[i]=='string'){
                            data[j][k] = String(data[j][k])
                        }else if(info[i]=='int'){
                            data[j][k] = Number(data[j][k]).toFixed(0)
                        }else if(info[i]=='float'){
                            data[j][k] = Number(data[j][k])
                        }else if(info[i]=='array'){
                            data[j][k] = Array(data[j][k])
                        }else if(info[i]=='object'){
                            data[j][k] = Object(data[j][k])
                        }
                    }
                }
            }
        }
        this.data = data
    }
    this.translate = function(info){
        this.info = info
        this.todo()
        return this.data
    }
    }
    var data = new Array(
        {
            id:'1',
            sex:0,
            name:'zhang',
            pics:new Array('a.png','b.png')
        },
        {
            id:'2',
            sex:'0',
            name:'li',
            pics:new Array('c.png','d.png')
        },
        {
            id:'5',
            sex:'true',
            name:'5',
            pics:new Array('e.png','f.png')
        }
    )
    var arr = {
        id:'int',
        sex:'bool',
        name:'string',
        pics:'array'
    }
    var translation = new translation(data)
    translation.init()
    var ret = translation.translate(arr)
    console.log(ret);
  2. 输出:
    [ { id: '1', sex: false, name: 'zhang', pics: [ [Array] ] },
    { id: '2', sex: true, name: 'li', pics: [ [Array] ] },
    { id: '5', sex: true, name: '5', pics: [ [Array] ] } ]

    二、php:

  3. 代码:
    ]>
    */
    class translation{
    private $data;
    private $info;
    public function __construct($data){
        $this->data = $data;
    }
    private function todo(){
        $data = $this->data;
        $info = $this->info;
        foreach($info as $k=>$v){
            foreach($data as $k1=>$v1){
                foreach($v1 as $k2=>$v2){
                    if($k==$k2){
                        if($v=='bool'){
                            $data[$k1][$k2] = (bool)$v2;
                        }elseif($v=='string'){
                            $data[$k1][$k2] = (string)$v2;
                        }elseif($v=='int'){
                            $data[$k1][$k2] = (int)$v2;
                        }elseif($v=='float'){
                            $data[$k1][$k2] = (float)$v2;
                        }elseif($v=='array'){
                            $data[$k1][$k2] = (array)$v2;
                        }elseif($v=='object'){
                            $data[$k1][$k2] = (object)$v2;
                        }
                    }
                }
            }
        }
        $this->data = $data;
    }
    public function translate($info){
        $this->info = $info;
        $this->todo();
        return $this->data;
    }
    }
    $data = array(
        array(
            id=>'1',
            sex=>'true',
            name=>'zhang',
            pics=>array('a.png','b.png')
        ),
        array(
            id=>'2',
            sex=>'true',
            name=>'li',
            pics=>array('c.png','d.png')
        ),
        array(
            id=>'5',
            sex=>'true',
            name=>'5',
            pics=>array('e.png','f.png')
        )
    );
    $arr = array(
        id=>'int',
        sex=>'bool',
        name=>'string',
        pics=>'array'
    );
    $translation = new translation($data);
    $data = $translation->translate($arr);
    var_dump($data);
  4. 输出:
    array(3) {
    [0]=>
    array(4) {
    ["id"]=>
    int(1)
    ["sex"]=>
    bool(true)
    ["name"]=>
    string(5) "zhang"
    ["pics"]=>
    array(2) {
      [0]=>
      string(5) "a.png"
      [1]=>
      string(5) "b.png"
    }
    }
    [1]=>
    array(4) {
    ["id"]=>
    int(2)
    ["sex"]=>
    bool(true)
    ["name"]=>
    string(2) "li"
    ["pics"]=>
    array(2) {
      [0]=>
      string(5) "c.png"
      [1]=>
      string(5) "d.png"
    }
    }
    [2]=>
    array(4) {
    ["id"]=>
    int(5)
    ["sex"]=>
    bool(true)
    ["name"]=>
    string(1) "5"
    ["pics"]=>
    array(2) {
      [0]=>
      string(5) "e.png"
      [1]=>
      string(5) "f.png"
    }
    }
    }

文章名称:javascript与php类型转换类
分享路径:http://jkwzsj.com/article/gejcdp.html

其他资讯