First, javascript:
- 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);
- 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:
- 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);
- 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