简体   繁体   English

将多个数组值转换为整数

[英]Converting several array values to integer

As shown in String to array of Integers php , there is a solution to convert ALL array elements to integer. String to Integers php数组所示,有一种解决方案可以将所有数组元素转换为整数。 But what if my array looks like this: 但是,如果我的数组看起来像这样:

$v=array(0.00, "0.00", "test", 50);

I need a converted array where all numeric elements (even if quoted, like "0.00") are converted to integer, but strings (like "test") must remain strings... 我需要一个转换后的数组,其中所有数字元素(即使被加引号,如“ 0.00”)都转换为整数,但是字符串(如“ test”)必须保留为字符串...

Try with array_map . 尝试使用array_map It will convert all the elements(except the strings you mentioned) to integer. 它将所有元素(除了您提到的字符串之外)转换为整数。

function convert_data($data) {
    if (is_numeric($data)) {
        $data = (int) $data;
    }
    return $data;
}
$ints[] = array_map('convert_data', $v);
var_dump($ints);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM