PHP代码ip2long 循环问题

WBOY
Release: 2016-06-23 13:38:53
Original
997 people have browsed it

print_r($dataIp);            foreach ($dataIp as $key=>$value) {                $data['ip']     =   $value;                $data['iplong']     =   bindec(decbin(ip2long($value)));                                            print_r($data);                         }
Copy after login


打印出来的结果为:

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)
Copy after login
Copy after login

但不知你想干什么

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

<?php$dataIp = array('208.104.70.215', '74.101.176.171', '73.17.35.30', '70.198.197.3', '46.44.179.76');foreach ($dataIp as $key=>$value) {		$data['ip']     =   $value;		$data['iplong']     =   bindec(decbin(ip2long($value)));                            		print_r($data);             }
Copy after login

输出
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)
Copy after login
Copy after login

也是正常的

如果你把 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);             }
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!