dongxin5054
2015-11-11 15:38
浏览 107
已采纳

显示数组中所有元素的数据类型

I have an array

array:23 [▼
  "cpe_mac" => "204492519985"
  "bandwidth_max_up" => 30000
  "bandwidth_max_down" => 50000
  "filter_icmp_inbound" => true
  "dmz_enabled" => false
  "dmz_host" => "http:\/\/ddd.com"
  "vlan_id" => 2
  "dns" => array:2 [▶]
  "xdns_mode" => 0
  "cfprofileid" => 11111
  "stub_response" => 0
  "acl_mode" => 1
  "portal_url" => "http:\/\/portal.com"
  "fullbandwidth_max_up" => 1000000
  "fullbandwidth_max_down" => 2000000
  "fullbandwidth_guaranty_up" => 300000
  "fullbandwidth_guaranty_down" => 400000
  "account_id" => 1000
  "location_id" => 3333
  "network_count" => 3
  "group_name" => "test_group"
  "vse_id" => 20
  "firewall_enabled" => false
]

I want to know the data type of each one of them, so I did this

$cpe_type = [];
foreach ($cpe as $k => $v) {
    $cpe_type[$k] = gettype($v);
}

I got as what I wanted

array:23 [▼
  "cpe_mac" => "string"
  "bandwidth_max_up" => "integer"
  "bandwidth_max_down" => "integer"
  "filter_icmp_inbound" => "boolean"
  "dmz_enabled" => "boolean"
  "dmz_host" => "string"
  "vlan_id" => "integer"
  "dns" => "array"
  "xdns_mode" => "integer"
  "cfprofileid" => "integer"
  "stub_response" => "integer"
  "acl_mode" => "integer"
  "portal_url" => "string"
  "fullbandwidth_max_up" => "integer"
  "fullbandwidth_max_down" => "integer"
  "fullbandwidth_guaranty_up" => "integer"
  "fullbandwidth_guaranty_down" => "integer"
  "account_id" => "integer"
  "location_id" => "integer"
  "network_count" => "integer"
  "group_name" => "string"
  "vse_id" => "integer"
  "firewall_enabled" => "boolean"
]

Is there any pre-made PHP functions that can provide me similar functionality?

图片转代码服务由CSDN问答提供 功能建议

我有一个数组

 数组:23 [▼\  n“cpe_mac”=>  “204492519985”
“bandwidth_max_up”=>  30000 
“bandwidth_max_down”=>  50000 
“filter_icmp_inbound”=>  true 
“dmz_enabled”=>  false 
“dmz_host”=>  “http:\ / \ / ddd.com”
“vlan_id”=>  2 
“dns”=>  array:2 [▶] 
“xdns_mode”=>  0 
“cfprofileid”=>  11111 
“stub_response”=>  0 
“acl_mode”=>  1 
“portal_url”=>  “http:\ / \ / portal.com”
“fullbandwidth_max_up”=>  1000000 
“fullbandwidth_max_down”=>  2000000 
“fullbandwidth_guaranty_up”=>  300000 
“fullbandwidth_guaranty_down”=>  400000 
“account_id”=>  1000 
“location_id”=>  3333 
“network_count”=>  3 
“group_name”=>  “test_group”
“vse_id”=>  20 
“firewall_enabled”=>  false 
] 
   
 
 

我想知道每一个的数据类型,所以我这样做了

 <  code> $ cpe_type = []; 
foreach($ cpe as $ k =&gt; $ v){
 $ cpe_type [$ k] = gettype($ v); 
} 
   
 
 

我得到了我想要的东西

  array:23 [▼
“cpe_mac”=&gt;  “string”
“bandwidth_max_up”=&gt;  “整数”
“bandwidth_max_down”=&gt;  “整数”
“filter_icmp_inbound”=&gt;  “boolean”
“dmz_enabled”=&gt;  “boolean”
“dmz_host”=&gt;  “string”
“vlan_id”=&gt;  “整数”
“dns”=&gt;  “array”
“xdns_mode”=&gt;  “整数”
“cfprofileid”=&gt;  “整数”
“stub_response”=&gt;  “整数”
“acl_mode”=&gt;  “整数”
“portal_url”=&gt;  “string”
“fullbandwidth_max_up”=&gt;  “整数”
“fullbandwidth_max_down”=&gt;  “整数”
“fullbandwidth_guaranty_up”=&gt;  “整数”
“fullbandwidth_guaranty_down”=&gt;  “整数”
“account_id”=&gt;  “整数”
“location_id”=&gt;  “整数”
“network_count”=&gt;  “整数”
“group_name”=&gt;  “string”
“vse_id”=&gt;  “整数”
“firewall_enabled”=&gt;  “boolean”
] 
   
 
 

是否有任何可以提供类似功能的预制PHP函数?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanbin198788 2015-11-11 15:40
    已采纳

    You can use array_map:

    var_dump(array_map('gettype', $array));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglang1976 2015-11-11 15:41

    ArrayMap with gettype as callback would be enough in your case.

    That would be the closest native implementation of what you would like to achieve.

    评论
    解决 无用
    打赏 举报
  • dourang20110122 2015-11-11 15:50

    From a debugging standpoint, var_dump will show you a presentable output of the types and values of any object in PHP.

    From a coding perspective, array_map is the best to transform an array. Simply provide it a callback, and it will transform all the values:

    array_map('gettype', $array);
    

    Here is a working phpplayground example.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题