在php中将字符串数组转换为整数数组

Fat*_*hah 1 php arrays

我有一个数组:

$TaxIds=array(2) { [0]=> string(1) "5" [1]=> string(2) "10" } 
Run Code Online (Sandbox Code Playgroud)

我需要转换为:

$TaxIds=array(2) { [0]=> int(5) [1]=> int(10) } 
Run Code Online (Sandbox Code Playgroud)

简单的方法???

小智 12

@george 答案的简化版本是:

$TaxIds = array_map('intval', $TaxIds);
Run Code Online (Sandbox Code Playgroud)

更多信息请访问array_map

虽然这个问题很久以前就被问过了,但它可能对某人有用


geo*_*rge 7

您可以使用array_map

$TaxIds = array_map(function($value) {
    return intval($value);
}, $TaxIds);
Run Code Online (Sandbox Code Playgroud)

  • 从 PHP 7.4 开始,可以使用箭头函数语法将其写在一行上:`$TaxIds = array_map(fn($value) => intval($value), $TaxIds);` (3认同)