1

This table consist of 3 column : key-name of product, total value, and rank. These ranks is sorted arsort based on the total value. How to echo only the name of product in the first row that has the biggest total value? How to access the key of the first row that consist of biggest value?

Here is the code of table of rank

<table class="table table-bordered table-striped table-hover">
    <tr>
        <th></th>
        <th>Total</th>
        <th>Rank</th>
    </tr>
    <?php     

    $rank = get_rank($pref);

    foreach($rank as $key => $value){
        echo"<tr>";
        echo"<th>$key - $PRODUK[$key]</th>";
        echo "<td class='text-primary'>".round($pref[$key], 3)."</td>";
        echo "<td class='text-primary'>".$rank[$key]."</td>";
        echo "</tr>";
        $no++;    
    }                            
    ?>
    </table>        

$key is the key and $PRODUK[key] is the name of the product. And their total value is $pref.

And here is the code for function of get rank

function get_rank($array){
    $pref= $array;
    arsort($pref);
    $no=1;
    $new = array();
    foreach($pref as $key => $value){
        $new[$key] = $no++;
    }
    return $new;
}
rafdwna
  • 19
  • 3
  • What's the point of function `get_rank`? Maybe to get an ordered *copy* of the array instead of re-ordering the array? Isn't using `arsort()` and then numbering the keys exactly the same as just using `sort()`? – Sgt AJ May 28 '16 at 03:29
  • Please show, how `$PRODUK` and `$pref` are initialized/generated. – Pinke Helga May 28 '16 at 03:32
  • If this array represents a table, I'm curious why you aren't simply using a query to get and sort the data the way you want. – Sgt AJ May 28 '16 at 03:36
  • Your question is not "a good question", having good chances to be downvoted and maybe deleted. Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Show, what actually is output. Explain the expected result. Show the **content of all the arrays inclusive keys**. Try `var_dump($variable)` – Pinke Helga May 28 '16 at 03:49

2 Answers2

0

There's no $PRODUK declared in your code snippet. You are iterating over $rank by foreach($rank as $key => $value). So the key is $key and a copy of $rank[$key] is in $value. Try to output $value instead or clarify your question and provide more code, where one can see what $PRODUK and its relation to $rank is.

As of your second snippet, the values the returned array are the rank, i.e. thay might represent the keys of the original array. So you could try to output e.g. round($pref[$value]) and $PRODUK[$value]. You might need to start with rank 0 since arrays commonly are based on zero. However, you do not provide the code of initialization.

Again: Your question does not provide enough information.

In order to your added question

How to echo only the name of product in the first row

$key = 1; // respective to your code $no=1;
$value = $rank[$key];

You do not need any foreach loop to access only a single element.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
0

It may help you

    $rank = get_rank($pref);
    echo"<tr>";
    echo"<th>".key($rank)."</th>";
    echo "<td class='text-primary'>".round($pref[key($rank)], 3)."</td>";
    echo "<td class='text-primary'>".reset($rank)."</td>";
    echo "</tr>";
Akshath
  • 86
  • 5