javascript与php类型转换类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript与php类型转换类相关的知识,希望对你有一定的参考价值。
一、javascript:
- 代码:
/** * 类型转换类 * @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);
- 输出:
[ { 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:
- 代码:
<?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);
- 输出:
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类型转换类的主要内容,如果未能解决你的问题,请参考以下文章
html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。
PHP和JavaScript将字符串转换为数字string2int