温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javascript与php类型转换类

发布时间:2020-07-12 13:18:06 来源:网络 阅读:422 作者:Lee_吉 栏目:web开发

一、javascript:

  1. 代码:
    /**
    * 类型转换类
    * @author [lee] <[<www.dollarphp.com>]>
    */
    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. 代码:
    <?php
    /**
    * 类型转换类
    * @author [lee] <[<www.dollarphp.com>]>
    */
    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"
    }
    }
    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI