Rumah >pembangunan bahagian belakang >tutorial php >字符串转化为数字?该如何解决

字符串转化为数字?该如何解决

WBOY
WBOYasal
2016-06-13 11:06:411070semak imbas

字符串转化为数字???
今天遇到个比较奇怪的现象,很可能是我储备还不够吧,包含引号的数字的字符串转化为数字时出现了问题

<br />"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"<br />

上面的为一段数据,接下来操作
<br />$arr = '"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"';<br />$arr = explode(',',$arr);<br />print_r($arr);<br />

打印
<br />Array<br />(<br />    [0] => "1000"<br />    [1] => "902865066306"<br />    [2] => "02865066306"<br />    [3] => "SIP Phone"<br />    [4] => "IP Trunk"<br />    [5] => ""<br />    [6] => "trunk20"<br />    [7] => "ON2OFF"<br />    [8] => "1333230069"<br />    [9] => "1333230072"<br />    [10] => "1333230134"<br />    [11] => "65"<br />    [12] => "62"<br />    [13] => "Connected"<br />)<br />

我想把数组里的第九个拿出来来和其他的数据比较,平常把引号包围的字符串转为数字时就是在后面加个0就ok了,可是,今天不一样了
<br />echo $arr[8] + 0; //结果为0<br />

我就纳闷了,怎么会为0呢,$arr[8]明明有值啊,于是我又测试了下
<br />$num = "1333230069";<br />echo $num+0;//1333230069<br />

弄了好久没明白怎么回事,后来就用了两个办法解决了,一个是用正则把数字提出来,另一个是一开始就把字符串里的双引号全都替换为空。

可是疑惑还没有解决,啰嗦到现在,就是想问问大大们,为什么上面的做法会出现0的结果,而不是想要的数字。

------解决方案--------------------
var_dump($arr[8]);  //string(12) ""1333230069""
var_dump($num);  //string(10) "1333230069"

这样看就清楚了,将 $arr[8] 的首尾双引号去掉后再运算就对了。
------解决方案--------------------
$a=str_getcsv($arr);
var_dump($a[8]);


------解决方案--------------------
根据你提供的信息我自己也测试了下:
<?php<br />$arr= Array<br />(0 => "1000",<br />    1 => "902865066306",<br />    2 => "02865066306",<br />    3 => "SIP Phone",<br />    4 => "IP Trunk",<br />    5 => "",<br />    6 => "trunk20",<br />    7 => "ON2OFF",<br />    8 => "1333230069",<br />    9 => "1333230072",<br />    10 => "1333230134",<br />    11 => "65",<br />    12 => "62",<br />    13 => "Connected"<br />);<br />echo gettype($arr[8])."<br>";<br />echo $arr[8]."<br>";<br />$str1=$arr[8]+0;<br />echo gettype($str1)."<br>";<br />echo ($str1)."<br>";<br /><br />$num = "1333230069";<br />$num1=($num+0);<br />echo gettype($num)."<br>";<br />echo gettype($num1)."<br>";<br />echo $num1;<br /><br />?><br />


输出结果是:
string
1333230069
integer
1333230069
string
integer
1333230069

发现没有出现楼主的问题。
------解决方案--------------------
没必要那么麻烦吧?

$arr = '"1000","902865066306","02865066306","SIP Phone","IP Trunk","","trunk20","ON2OFF","1333230069","1333230072","1333230134","65","62","Connected"';<div class="clear">
                 
              
              
        
            </div>
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php include 异常Artikel seterusnya:php getimagesize解决办法