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); }
如果填入第五行数字里面的IP到表单后提交,不管是否注释第五行,打印出来的数组都是一样的。
但是循环的结果却不同,结果见图。我真不知道怎么会出现这个问题,纠结了好一阵。
多谢楼上几位找到问题了,是表单里面的前后空格导致获取不到值。

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
