PHP代碼ip2long 迴圈問題

來源:互聯網
上載者:User
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到表單後提交,不管是否注釋第五行,列印出來的數組都是一樣的。
但是迴圈的結果卻不同,結果見圖。我真不知道怎麼會出現這個問題,糾結了好一陣。

多謝樓上幾位找到問題了,是表單裡面的前後空格導致擷取不到值。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.