【问题标题】:Most efficient way to count the number of duplicate elements in PHP计算PHP中重复元素数量的最有效方法
【发布时间】:2012-05-06 00:06:08
【问题描述】:

假设我们有一组学生:

学生:本·萨姆 ...

每个学生家里都有一组书:

书籍:

  • 山姆 A B C D
  • Ben D E F
  • 瑞恩 A D G D

所以在 PHP 中,$students 数据结构是这样的:

Array
(
    [0] => Array
        (
            [name] => Joe Smith
            [books] => Array
                (
                    [0] => A
                    [1] => B
                    [2] => C
                )

        )

)

我们想计算 A 被重复了多少次以及谁拥有 A B 和 C 等。 在 php 中执行此操作的一种方法是:

if (sizeof($potential_entries) > 0) :
  for ($j = 0, $potentialsize = sizeof($potential_entries); $j < $potentialsize; ++$j)     {
    for ($k = 0, $listsize = sizeof($student_book); $k < $listsize; ++$k) {
      if ($student_book[$k] == $potential_entry[$k]) :
        $match = 1;
        $student_book[$k]['like_count']++;
       endif;
    }
  }

这不是很有效,我们宁愿想要一个映射结构或一个哈希表,php 有像 perl 这样的结构吗?还是我们必须手动构建它们。 编辑: 我可以在 PHP 中看到我们有关联数组?您能否举一个在文档中找不到的示例。 对于山姆:

var_dump($potential_entry):

[0]=>array(2) { ["name"]=> string(1) "A" ["id"]=> string(11) "1348"}
[1]=>array(2) { ["name"]=> string(1) "B" ["id"]=> string(11) "1483"}
[2]=>array(2) { ["name"]=> string(1) "C" ["id"]=> string(11) "13"}
[3]=>array(2) { ["name"]=> string(1) "D" ["id"]=> string(11) "1174"}

这是 Sam 的,其余的我们有相同的结构。所以 sam 是数组,books 是数组,sam 有很多书。 在这个例子中

  • D count=3 山姆·本·瑞恩
  • 计数=2 sam ryan
  • 等等...

【问题讨论】:

  • 我不明白,PHP有关联数组,那是地图的别称……
  • 你能用一个例子/提示澄清一下吗...我可以在 php 中看到关联数组,但我不确定如何表示我的列表。
  • 我的意思是如何在 php 中推送和弹出到哈希数组
  • sizeof 可以替换为count 我认为或者如果$potential_entries 始终是一个数组,那么只需if ( $potential_entries ) 应该检查它是否为空。
  • 你能告诉我们var_dump($potential_entries)的输出吗?

标签: php arrays hashmap hashtable


【解决方案1】:

如果您将 $students 作为一个数组,并且该数组中的每个条目都有一个名为 books 的书籍数组

$books = array(); 
for ($i = 0, $count = sizeof($students); $i++) {
   foreach ($student[$i]['books'] as $book) { 
      if (isset($books[$book])) { 
          $books[$book]++; 
      } else { 
          $books[$book] = 1;
      }
   }
}

然后在此之后,任何计数大于 1 的 $books 条目都是重复的,即

foreach ($books as $key => $value) { 
   if ($value) > 1) { 
        echo "Book " . $key . " is a duplicate (" . $value . ")"; 
   }
}

【讨论】:

  • 外部的for 也可以是foreach
  • @cyperhable - 绝对。我假设有一个数字索引,但如果没有,foreach 将是要走的路。
  • @ScottWilson 我不太明白这一行: if (isset($books[$book])) { $books[$book]++; } 其他 { $books[$book] = 1; }
  • isset($books[$book]) 是做什么的?
  • $books 是一个关联数组,需要检查是否已经设置了相关索引。
【解决方案2】:
$aFullCount = array(); // in this array you will have results of all characters count.
$aCondition = array("A", "B"); // set condition you need to count down here.
$aConditionMatch = array(); // in here you will have all users ids who matched your A + B
if ($aUsers){
    foreach ($aUsers as $sUserKey=>$aOneUser){
        $aForCondition = array();
        if ($aPotentialEntries){
            foreach ($aPotentialEntries as $aOnePotentialEntry){
                $aForCondition[] = $aOnePotentialEntry["name"];
                if (!isset($aFullCount[$aOnePotentialEntry["name"]]))
                    $aFullCount[$aOnePotentialEntry["name"]] = 1;
                else
                    $aFullCount[$aOnePotentialEntry["name"]]++;
            }
        }

        $aConditionCheck = array_intersect($aForCondition, $aCondition);
        if (!array_diff($aConditionCheck, $aCondition))
            $aConditionMatch[] = $sUserKey;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2012-04-15
    • 2020-12-01
    相关资源
    最近更新 更多