Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将整数数组转换为整数_Php - Fatal编程技术网

在php中将整数数组转换为整数

在php中将整数数组转换为整数,php,Php,我正在使用这段代码,将整数数组转换为整数 return intval(implode("",$result)); 然后从 [5,6,7] 我明白了 有一种更直接的转换方法吗?您可以使用intval或cast,但您可以不使用任何字符串进行转换 您可以使用数组_reduce和两种不同的方法字符串串联或数学逻辑: 弦缩合 $nums=[5,6,7]; $result=(int)数组_reduce($nums,function($acc,$el){ $acc.=$el; 返回$a

我正在使用这段代码,将整数数组转换为整数

return intval(implode("",$result));
然后从

[5,6,7]
我明白了


有一种更直接的转换方法吗?

您可以使用
intval
cast
,但您可以不使用任何字符串进行转换


您可以使用
数组_reduce
和两种不同的方法字符串串联数学逻辑

弦缩合
$nums=[5,6,7];
$result=(int)数组_reduce($nums,function($acc,$el){
$acc.=$el;
返回$acc;
},'');
回声$结果//567
/*
=> ''
''.5 => '5'
'5'.6 => '56'
'56'.7 => '567'
*/
数学逻辑
$index=count($nums)-1//2.
$result=array_reduce($nums,function($acc,$el)use(&$exponent){
$acc+=$el*10**$指数;
美元指数--;
返回$acc;
}, 0);
回声$结果//567
/*
=> 0
5 * 10**2  =>  500
6 * 10**1  =>  560
7 * 10**0  =>  567
*/

您当前的方法有什么问题?您能得到多少更直接的结果?您可以使用
foreach
循环或
array\u reduce()
来计算结果,但不会更清楚。
返回(int)内爆($result)更短。
567