Question

I have an array of posts that I want to sort - but before I do, I want to find the id of the post with the highest number of likes.

I loop through the array using a foreach. Although it seems like a waste to do two foreach loops for this - I don't know if there's an alternative when trying to find the highest value beforehand?

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

)

So the second post has the highest amount of likes, so I need the ID of 165 - then when I loop through I can do something like

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

Any help would be much appreciated - thanks!

Was it helpful?

Solution

$highest = 0;
$highest_id = 0;

foreach($array as $a) {

    if($a['like_count'] > $highest) {

        $highest = $a['like_count'];
        $highest_id = $a['id'];
    }
}

Hope I understood you correctly :)

OTHER TIPS

This looks like data retrieved from a database. If so, use the ORDER BY like_count DESC clause in the SQL.

The ID of the post with the most likes will then be at $posts[0]['id'] before you sort by your other method.

very easy task, you loop through your posts.

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']
    }

You could use 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

Try this:

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

Where $posts is the input array.

Explanation:

  • first you sort the posts by like count in decreasing fashion
  • then you get the top post id if there any posts, null otherwise.

What's good about this solution is that you can also select first n posts.

$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]

Is those datas came from a database like MySQL? If is it, the simpliest solution is to put an "ORDER BY".

You can also make seperate the 'like_count' array keeping the same key than your first array and doing a asort (http://www.php.net/manual/fr/function.asort.php). You will have the key of the highest like count.

You can sort the array in descending order based on like_count and then pick up the id for the first array element.

You can use max function to get the highest value of likes:

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

echo max($max['id']);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top