【问题标题】:find id of post with highest value查找具有最高值的帖子的 id
【发布时间】:2013-06-23 06:29:20
【问题描述】:

我有一系列要排序的帖子 - 但在此之前,我想找到 likes 数量最多的帖子中的 id

我使用 foreach 遍历数组。虽然为此执行两个 foreach 循环似乎很浪费 - 我不知道在尝试预先找到最高值时是否有替代方案?

Array
(
    [0] => Array
        (
            [id] => 162
            [like_count] => 2
            etc.
        )
    [1] => Array
        (
            [id] => 165
            [like_count] => 23
            etc.
        )

)

所以第二个帖子的点赞数最高,所以我需要 165 的 ID - 然后当我循环通过时,我可以做类似的事情

foreach ($posts as $post){
    if($most_liked_id == $post["id"]){
       // this post is the most liked!
    }
}

任何帮助将不胜感激 - 谢谢!

【问题讨论】:

  • 两个foreach循环是什么意思?

标签: php arrays max usort asort


【解决方案1】:

这看起来像是从数据库中检索到的数据。如果是这样,请在 SQL 中使用ORDER BY like_count DESC 子句。

在您按其他方法排序之前,获得最多赞的帖子 ID 将位于 $posts[0]['id']

【讨论】:

    【解决方案2】:

    非常简单的任务,您可以循环浏览您的帖子。

    function get_max_like_post($posts) {
        $max_like = 0;
        $max_like_post = array();
        foreach ($posts as $post) {
            if ($post['like_count'] > $max_like) {
                $max_like = $post['like_count'];
                $max_like_post = $post;
            }
        }
    
        return $max_like_post['id']
        }
    

    【讨论】:

      【解决方案3】:
      $highest = 0;
      $highest_id = 0;
      
      foreach($array as $a) {
      
          if($a['like_count'] > $highest) {
      
              $highest = $a['like_count'];
              $highest_id = $a['id'];
          }
      }
      

      希望我理解正确:)

      【讨论】:

      • 选择这个作为答案是它是最干净和最简单的方法。 :) 谢谢@paranoid。
      • 哦,它不会从我用来对数据进行排序的原始 SQL 查询中重新排列数组。
      【解决方案4】:

      你可以使用usort

      $posts = array(
          array('id' => 161, 'like_count' => 0),
          array('id' => 162, 'like_count' => 6),
          array('id' => 4, 'like_count' => 2),
      );
      
      function like_sort($a, $b) {
          if ($a['like_count'] == $b['like_count']) {
              return 0;
          }
          return ($a['like_count'] > $b['like_count']) ? -1 : 1;
      }
      usort($posts, 'like_sort');
      
      // The first element in the array is now the one with the highest like_count.
      echo $posts[0]['id']; // 162
      

      【讨论】:

      • 谢谢,这很好用,但不幸的是我不想重新排列数组,因为它的顺序是由其他东西定义的。
      • 如果你想保留顺序以便以后以原始形式使用数组,我想你总是可以先复制它($posts_ordered = $posts)。但是(在我的示例中)如果 $posts 大小合适,我们将开始变得非常低效,并且提出的其他解决方案会更好。
      【解决方案5】:

      试试这个:

      usort($posts, function($item) { return -$item['like_count']; });
      $id = empty($posts) ? null : $posts[0]['id'];
      

      其中$posts 是输入数组。

      解释:

      • 首先您按点赞数以递减的方式对帖子进行排序
      • 如果有任何帖子,您将获得顶部的帖子 ID,否则为 null。

      此解决方案的好处是您还可以选择前 n 个帖子。

      【讨论】:

        【解决方案6】:
        $highest_post_likes = 0;
        $highest_post_id = 0;
        for($i = 0; $i < sizeof($posts); $i++) {
            if ( $posts[$i][like_count] > $highest_post_likes ) {
                $highest_post_likes = $posts[$i][like_count];
                $highest_post_id = $i;
            }
        }
        
        // now you can use $posts[$highest_post_id]
        

        【讨论】:

          【解决方案7】:

          您可以使用max函数获取最高点赞数:

                  foreach ($posts as $post){
                  $max[]=$post['like_count'];
              }
          
          echo max($max['id']);
          

          【讨论】:

          • 谢谢,虽然这不会返回具有最高价值的帖子的id
          【解决方案8】:

          这些数据是否来自 MySQL 之类的数据库?如果是这样,最简单的解决方案是放一个“ORDER BY”。

          您还可以将“like_count”数组分开,保持与第一个数组相同的键并进行排序 (http://www.php.net/manual/fr/function.asort.php)。您将拥有最高点赞数的钥匙。

          【讨论】:

            【解决方案9】:

            您可以根据like_count 对数组进行降序排序,然后选择id 作为第一个数组元素。

            【讨论】:

              猜你喜欢
              • 2021-09-23
              • 2019-04-11
              • 1970-01-01
              • 2023-03-16
              • 2015-07-08
              • 2016-07-05
              • 2016-05-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多