在PHP中,我需要按顺序将TLD,2LD等插入到数组中

时间:2016-01-19 06:47:56

标签: php arrays sorting

我有一个包含域列表,2LD等的文件。我在PHP中将其读入一个数组。 我有另一个列表,其中包含第一个列表中可能缺少的域名。我把它读到另一个数组中。 第一个数组的简化版本如下所示:

.com
.com.au
.tas.gov.au
.vic.gov.au
.wa.gov.au
.gov.au
.au
.co.uk

A sample from the second list to insert alphabetically:

.org.au
.wa.au
.sa.gov.au


So .org.au would have to be inserted after .gov.au
and .wa.au would have to be inserted after that.
and .sa.gov.au would need to be inserted before .tas.gov.au

我想我可能需要爆炸列表并制定两个不同的规则,每个域级别一个(例如.gov.au,.tas.gov.au)并分别处理它们。然后,根据它具有的级别数,按字母顺序比较最左侧的值。 (STRCMP)。只是,值必须插入另一批相同级别的值之上。 (例如 .org.au 不会追溯 .com.au .wa.au 不会追溯 .wa .gov.au

有什么想法吗?

由于“您的帖子似乎包含代码”

,因此无法在不缩进随机部分的情况下发布此内容

1 个答案:

答案 0 :(得分:0)

好的,所以我看到它的方式,你最好的选择是以相反的顺序存储你的域,以便按字母顺序排序。最简单的方式,即imo(虽然效率不高),只需要一个大型数组,您的域名将被还原。所以插入你喜欢

.wrapper {
  background-color: white;
  min-height: 200px;
  width: 200px;
  float: left;
  margin-left: 50px;
  display: flex;
  flex-direction: column;
}

.wrapper .fixed-height {
  background-color: orange;
  min-height: 30px;
}

.wrapper .second {
  background-color: yellow;
  min-height: 30px;
}

.wrapper .variable-height {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.wrapper .variable-height .text-container {
  width: 100%;
  text-align: center;
}

然后显示您需要重新还原它的特定域:

<div class="wrapper">
  <div class="fixed-height"></div>
  <div class="fixed-height second"></div>
  <div class="variable-height">
    <div class="text-container">
      <div class="title">Title</div>
      <div class="subtitle">Subtitle</div>
    </div>
  </div>
  <div class="fixed-height"></div>
</div>

<div class="wrapper">
  <div class="fixed-height"></div>
  <div class="variable-height">
    <div class="text-container">
      <div class="title">Title</div>
      <div class="subtitle">Subtitle</div>
    </div>
  </div>
  <div class="fixed-height"></div>
</div>

这种天真的实现应该对相对较少的条目(比如几千条)有效,除非我做出任何错字。我将$toAdd = join(".", array_reverse(explode(".", $domain)) . ".|"; array_push($allDomains, $toAdd); array_sort($allDomains); 添加到字符串的末尾,以便$domain = $allDomains[$i]; echo array_reverse(explode(".", str_replace($domain, ".|"))); ".|"之后排序,如果您不关心根域在其子排序之前排序,只需删除该部分。

我认为,更有效的方法是采用树形结构。数据看起来像这样:

.au.|

然后,要插入一个新域,您将导航树,将任何新的更高域添加为数组,将“leaf”作为bool。有时您需要使用包含.au.com.|的新数组替换bool。每次向数组添加密钥时,都'au' => array( 'com' => true, 'gov' = array( 'tas' => true, 'vic' => true, 'was' => true, '|_ROOT' => true, [...] 。我再次使用'|_ROOT' => true在根域之前对根域进行排序。

显示您的域应该是走树的简单问题。如果您需要,我可以建议您实施。