maxlcoder/data-type

数据类型转换器

dev-master 2017-10-19 02:19 UTC

This package is auto-updated.

Last update: 2024-09-05 01:20:18 UTC


README

数组或对象属性类型转换

配置

需要转换的属性与类型

支持类型有

  • "布尔" 或 "bool"
  • "整数" 或 "int"
  • "浮点" 或 "double"
  • "字符串"
  • "数组"
  • "对象"
  • "null"

示例

// 举例
$configs = [
  'username' => 'string',
  'age' => 'int',
];
$datas = [
  'username' => 'Lily', // string
  'age' => '12', // string 
];
var_dump($datas);
/*
array(2) {
  ["username"]=>
  string(4) "Lily"
  ["age"]=>
  string(2) "12"
}
*/
$app = new DataType($configs);
$results = $app->convert($datas);
var_dump($results);
/*
array(2) {
  ["username"]=>
  string(4) "Lily"
  ["age"]=>
  int(12)
}
*/