php按ip地址对URL排序

php按ip地址对URL排序,php,Php,好的,我之前发布了一个不太清楚的问题,让我再试一次。 我正试图将我的网站分为5个批次,每个批次都有不同的ip地址 要做到这一点,我必须为每个url动态获取IP,然后将它们、站点组织成每批5个站点,每个站点都有自己独特的IP地址 如果我有多个IP地址,它们必须在下一批中显示 有人能帮我解决这个问题吗 我有一个带有站点和IP的阵列 这是我的密码: if(isset($Organize_List)){ $List = explode("\n", $Organize_List); $IP_Arra

好的,我之前发布了一个不太清楚的问题,让我再试一次。 我正试图将我的网站分为5个批次,每个批次都有不同的ip地址

要做到这一点,我必须为每个url动态获取IP,然后将它们、站点组织成每批5个站点,每个站点都有自己独特的IP地址

如果我有多个IP地址,它们必须在下一批中显示

有人能帮我解决这个问题吗

我有一个带有站点和IP的阵列

这是我的密码:

if(isset($Organize_List)){
 $List =   explode("\n", $Organize_List);
$IP_Array = array();
      foreach($List as $SIte){
    $Ip = gethostbyname(trim($SIte));
      if(preg_match('/^\d+$/', $Ip[1])){
         $IP_Array[$Ip][] = $SIte.'<br />';
                }
 }
if(isset($Organize_List)){
$List=分解(“\n”,$Organize\u List);
$IP_数组=数组();
foreach($SIte列为$SIte){
$Ip=gethostbyname(trim($SIte));
if(preg_match('/^\d+$/',$Ip[1])){
$IP_数组[$IP][]=$SIte.“
”; } }
现在这里是它变得棘手的地方


我的问题是如何将它们组织成每批5个站点的批次,每个站点都有自己的唯一IP地址。
解析url
gethostbyname
foreach
。足够让您开始了,如果您的实际代码遇到任何问题,请回来。我已经做了所有这些,我的问题是没有得到每个站点都有一个e信息,但是,在我上面提到的庄园中对齐它们。首先感谢您的快速重播。代码弹出关闭了这个错误:“Dns查询失败”如果我尝试将它与gethostbyname一起使用,这会不会同样有效?dns是我用来从中获取ip地址的方式,gethostbyname应该可以,只需替换“$current_dns_record=”-行和“$current_ip=”-行编辑。同样,它会弹出此消息:未定义索引:199.96.156.205您使用的错误级别是什么?在哪一行错误或警告?应该出现在for循环中,因为在我添加值之前我不会检查它是否存在,而在while循环中则不应该出现,因为它不起作用,出现了许多错误,无论如何,谢谢。我接受你的重播,因为你是唯一一个能够尝试的人!谢谢!
/* your base data */
$domains = array(/* your data */);

/* a list of domains per ip number, like $domain_by_ip['64.34.119.12'] = 'stackoverflow.com' */
$domain_by_ip = array();

/* a list counting number of domains by ip number */
$ip_count = array();

/* a list of domains we faild to fetch ip number for */
$failed = array();

/* loop through all doains */
foreach($domains as $current_domain)
{
   /* fetch the A record for all domains */
   $current_dns_record = dns_get_record($current_domain, DNS_A);

   /* if there is a dns record */
   if($current_dns_record)
   {
      /* fetch ip from result */
      $current_ip = $current_dns_record[0]['ip'];

      /* thos row is not needed, but php may triggering a warning oterhwise */
      if(!isset($domain_by_ip[$current_ip])) $domain_by_ip[$current_ip] = array();
      if(!isset$ip_count[$current_ip])) $ip_count[$current_ip] = array();

      /* add domain to the list by ip */
      $domain_by_ip[$current_ip][] = $current_dns_record;

      /* count up the count of domains on this ip */
      $ip_count[$current_ip]++;
   }
   else
   {
      /* if there was no dns record, put this domain on the fail list */
      $failed[] = $current_domain;
   }
}

/* create a list for storing batches */
$batches = array();

/* as long as we have ip-numbers left to use */
while($ip_count)
{
   /* create a list for storing current batch */
   $current_batch = array();

   /* sort ip-numbers so we take the ip-numbers whit most domains first */
   arsort($ip_count);

   /* take the top 5 ip-numbers from the list */
   $current_batch_ip_list = array_slice(array_keys($ip_count), 0, 5);

   /* foreach of thous 5 ip-numbers .. */
   foreach($current_batch_ip_list as $current_ip)
   {
      /* move one domain from the domain by ip list to the current batch */
      $current_batch[] = array_pop($domain_by_ip[$current_ip]);

      /* count down the numbers of domains left for that ip */
      $ip_count[$current_ip]--;

      /* if there is no more domains on this ip, remove it from the list */
      if($ip_count[$current_ip] == 0)
      {
         unset($ip_count[$current_ip]);
      }
   }

   /* add current batch to the list of batches */
   $batches[] = $current_batch;
}