Rumah >pembangunan bahagian belakang >tutorial php >PHP代码ip2long 循环问题
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)
版主大人有没有改代码,我在本地试了无数次都只有读出最后一个。不知道是什么原因。
<?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); }
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); }
多谢楼上几位找到问题了,是表单里面的前后空格导致获取不到值。