0

I have a table that I need to find "top trusted builder" from, Trusts are separated by " ; ", So I need to pull all the data, explode the usernames, order and count them, and Im stuck on finally outputting the top username form the array, I have posted up the full segment of PHP, because im sure there has to be a much better way of doing this.

<?php 
$GTB = $DBH->query('SELECT builders from griefprevention_claimdata where builders <> ""');
$GMCB->setFetchMode(PDO::FETCH_ASSOC);

$buildersarray = array();

while($row = $GTB->fetch()) {

      $allbuilders = explode(";", $row['builders']);
        for($i = 0; $i < count($allbuilders); $i++)
          {
            $buildersarray[] = $allbuilders[$i];
          }
}

echo "<pre>";
print_r(array_count_values(array_filter($buildersarray)));
echo "</pre>";
?>

Outputs

Array
(
[username1] => 4
[username2] => 1
[username3] => 1
)

2 Answers 2

1

You can return the array from the array_count_values command into a new array called $topbuilders, and then echo out the current (first) record:

$topbuilders = array_count_values(array_filter($buildersarray));
echo current(array_keys($topbuilders)).', Rank = '.current($topbuilders);

current() will return the first item in an associative array.

Sign up to request clarification or add additional context in comments.

5 Comments

This partially worked, output was "0, Rank = username1 " Im going to need to display their amount of trusts too..
Not sure what you mean by their "amount of trusts." Is that stored somewhere in this existing fetched array, or is that something that needs to come from a separate query?
Ok, making sense now - I edited my answer. Try it out.
This is it, Superb! Im going to now research Current, I had never heard of it. Thanks again, I was really stuck on getting this to work!
Hey IarsAnders, After using the segment of code with a larger sample size of data, it dousnt seems to be reporting the "most trusted" (from "builders") as a user called wkeresey, this user isnt the moste trusted. Now im stuck as I thought we managed to fix it! To help I have exported my database for you to see the structure too! pastebin.com/VpVAxAMF
0

Instead of using array_count_values() it would be better to use the names as the key to a frequency array:

$GTB = $DBH->query('SELECT builders from griefprevention_claimdata where builders <> ""');

$result = array();

while (($builders = $GTB->fetchColumn()) !== false) {
    foreach (explode(";", $builders) as $builder) {
        if (isset($result[$builder])) {
            $result[$builder]++;
        } else {
            $result[$builder] = 1;
        }
}
// sort descending based on numeric comparison
arsort($result, SORT_NUMERIC);

echo "Name = ", key($result), " Count = ", current($result), "\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.