0

I am working on a crytomining page that shows all the miners that are active. Right now, it's displaying from an API in PHP:

<table class="left">
        <tr><th colspan="3">Connected Miners (POOL)</th></tr>
        <tr><td class="header">Miners Address</td><td class="header">Shares</td><td class="header">Miners Hashrate</td></tr>
        <?php
        $poolminers = json_decode(file_get_contents("<<API LINK>>"), false);
        foreach ($poolminers->body->primary->shared as $miners) {
            echo '<tr><td><a href=<<LINK>>?wallet=' . $miners->worker . '>' . $miners->worker . '</a></td>';
            echo '<td>' . round($miners->shares, 2) . '</td><td>';
            if ($miners->hashrate / 1000000 < 1000) {
                echo round($miners->hashrate / 1000000, 3) . ' MH/s</td></tr>';
            }
            if ($miners->hashrate / 1000000 > 1000) {
                echo round($miners->hashrate / 1000000000, 3) . ' GH/s</td></tr>';
            }
        }
        ?>
</table>

I want to sort by the miner's hashspeed, but I'm unsure how to sort by the "next" value. I tried putting $poolminers into an array, but I received a fatal error for -

Fatal error: Can't use function return value in write context

Side note - the owner of the site does not want the site info given out, so I add <<>> where the actual links are - sorry for any confusion.


So after reading the first answer, I'm wondering if maybe my issue is not so much the sorting (although that is the primary concern) but more of how do I convert an API's data into a multi-dimensional array?

1 Answer 1

1

You can use usort function to make this happen: usort

This code should sort your data by hashrate in ascending order:

$json = '[{"hashrate":50},{"hashrate":10},{"hashrate":35},{"hashrate":20}]';
$poolminers = json_decode($json, false);

usort($poolminers, function ($a, $b) {
    return $a->hashrate <=> $b->hashrate;
});

foreach ($poolminers as $miners) {
    echo $miners->hashrate . PHP_EOL;
}

Output:

10
20
35
50

If you would like to sort by descending order just swap $a and $b like this:

usort($poolminers, function ($a, $b) {
    return $b->hashrate <=> $a->hashrate;
});

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

3 Comments

I took out the hard coded $json and put it back to the original decode of json_decode (since it's not constants) and this is what I got... Fatal error: Uncaught TypeError: usort(): Argument #1 ($array) must be of type array, stdClass given in C:\xampp\htdocs\ravenmine\Pool\miners.php:51 Stack trace: #0 C:\xampp\htdocs\ravenmine\Pool\miners.php(51): usort(Object(stdClass), Object(Closure)) #1 {main} thrown in C:\xampp\htdocs\ravenmine\Pool\miners.php on line 51
That's because usort needs an array as a first argument and you passing an object. You can either cast this object to associative array by using (array) $object or just use json_decode($data, true) to get an array.
I did figure out the "true" part eventually - I still couldn't really get it to work the way I wanted, but it stopped erroring. Marking as answer accepted, but I ended up going the really old fashioned way (a=b b=c c=a).

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.