如何从数组中创建整数?

时间:2016-07-19 12:23:40

标签: php arrays

是否可以将数组值转换为一个整数。例如,我有数字的数组

$array = array(7,4,7,2);

是否可以从此数组中获取整数值7472?

5 个答案:

答案 0 :(得分:5)

简单使用 implode 作为

$array = array(7,4,7,2);
echo (int)implode("",$array);// 7472

答案 1 :(得分:2)

使用implode,它从数组中创建一个字符串。 http://php.net/manual/en/function.implode.php

echo implode($array);

答案 2 :(得分:2)

使用implode函数,因为它从数组中创建一个字符串并尝试:

echo implode("",$array);

答案 3 :(得分:1)

使用implode(int)将字符串结果转换为整数:

$a = [7,4,7,2];
$res = (int) implode('', $a);

P.S。从PHP 5.4开始,您还可以使用短数组语法,它将array()替换为[]。

答案 4 :(得分:0)

# imports belong at the *top* of the file
import sys


class SomeDescriptiveError(Exception): pass
class SomeOtherSpecialError(Exception): pass


def func1(arg1 , arg2):
     if (some_cond):
        raise SomeDescriptiveError('Cannot frobnosticate the fizzbuzz')
     return arg1 + arg2
     # or skip the return statement altogether

def func2(arg1):
     if (some_cond):
        raise SomeOtherSpecialError('The frontobulator is no longer cromulent')
     return ''.join(reversed(arg1))

def main():
    print(func1(val1 , val2))
    print(func2(val3))

if __name__ == "__main__":
     try:
         result = main()
     except SomeDescriptiveError as e:
         print('Oh dear')
         sys.exit(e.args[0])
     except SomeOtherSpecialError as e:
         print('Oh no')
         sys.exit(e.args[0])
     else:
         print('All systems are fully operational')
     finally:
         print('I really should clean up all these bits.')