欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP代码ip2long 循环问题

程序员文章站 2024-04-04 10:46:35
...
print_r($dataIp);            foreach ($dataIp as $key=>$value) {                $data['ip']     =   $value;                $data['iplong']     =   bindec(decbin(ip2long($value)));                                            print_r($data);                         }


打印出来的结果为:

Array
(
[0] => 208.104.70.215
[1] => 74.101.176.171
[2] => 73.17.35.30
[3] => 70.198.197.3
[4] => 46.44.179.76
)
====
Array
(
[ip] => 208.104.70.215
[iplong] => 0
)
Array
(
[ip] => 74.101.176.171
[iplong] => 0
)
Array
(
[ip] => 73.17.35.30
[iplong] => 0
)
Array
(
[ip] => 70.198.197.3
[iplong] => 0
)
Array
(
[ip] => 46.44.179.76
[iplong] => 774681420
)


只有最后一个转换成功了,其他的都失败了,这是什么原因?


回复讨论(解决方案)

ip2long不能对数组转换吧?

没有问题

Array(    [ip] => 208.104.70.215    [iplong] => 3496494807)Array(    [ip] => 74.101.176.171    [iplong] => 1248178347)Array(    [ip] => 73.17.35.30    [iplong] => 1225859870)Array(    [ip] => 70.198.197.3    [iplong] => 1187431683)Array(    [ip] => 46.44.179.76    [iplong] => 774681420)

但不知你想干什么

版主大人有没有改代码,我在本地试了无数次都只有读出最后一个。不知道是什么原因。

$value) {		$data['ip']     =   $value;		$data['iplong']     =   bindec(decbin(ip2long($value)));                            		print_r($data);             }

输出
Array(    [ip] => 208.104.70.215    [iplong] => 3496494807)Array(    [ip] => 74.101.176.171    [iplong] => 1248178347)Array(    [ip] => 73.17.35.30    [iplong] => 1225859870)Array(    [ip] => 70.198.197.3    [iplong] => 1187431683)Array(    [ip] => 46.44.179.76    [iplong] => 774681420)

也是正常的

如果你把 print_r($data); 放到循环外面的话,就只有最后一个了

???,正常。

我的完整代码是这样的

$dataIp= explode("\n", $_POST['ip']);		if(empty($dataIp)){	$this->error('未填写IP!');}//$dataIp = array('208.104.70.215', '74.101.176.171', '73.17.35.30', '70.198.197.3', '46.44.179.76');print_r($dataIp);foreach ($dataIp as $key=>$value) {        $data['ip']     =   $value;        $data['iplong']     =   bindec(decbin(ip2long($value)));                                    print_r($data);             }

如果填入第五行数字里面的IP到表单后提交,不管是否注释第五行,打印出来的数组都是一样的。
但是循环的结果却不同,结果见图。我真不知道怎么会出现这个问题,纠结了好一阵。

多谢楼上几位找到问题了,是表单里面的前后空格导致获取不到值。