JavaScript and PHP type conversion classes

Source: Internet
Author: User

First, javascript:
  1. Code:
    /*** type Conversion class * @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 = t                    His.info for (var i-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 (in                        fo[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) Translati On.init () var ret = translation.translate (arr) console.log (ret);
  2. Output:
    [ { id: ‘1‘, sex: false, name: ‘zhang‘, pics: [ [Array] ] },{ id: ‘2‘, sex: true, name: ‘li‘, pics: [ [Array] ] },{ id: ‘5‘, sex: true, name: ‘5‘, pics: [ [Array] ] } ]
    Second, PHP:
  3. Code:
    <?php/*** type Conversion class * @author [Lee] <[<www.dollarphp.com>]>*/class translation{private $data;p rivate $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 $k 1=> $v 1) {foreach ($v 1 as $k 2=> $v 2) {                    if ($k = = $k 2) {if ($v = = ' bool ') {$data [$k 1][$k 2] = (bool) $v 2;                    }elseif ($v = = ' string ') {$data [$k 1][$k 2] = (string) $v 2;                    }elseif ($v = = ' int ') {$data [$k 1][$k 2] = (int) $v 2;                    }elseif ($v = = ' float ') {$data [$k 1][$k 2] = (float) $v 2;                    }elseif ($v = = ' array ') {$data [$k 1][$k 2] = (array) $v 2;                    }elseif ($v = = ' object ') {$data [$k 1][$k 2] = (object) $v 2;         }                }   }}} $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 TR Anslation ($data); $data = $translation->translate ($arr); Var_dump ($data);
  4. Output:
    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 and PHP type conversion classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.