3

Hey i have an associative array which has keys as String and values as Int. So from that associative array i need to get the key which has the highest value and if multiple keys have the same value then i need the key with the highest length.

So whats the most efficient way of doing this?

example

array = (
    'abc'     => 10,
    'def'     => 8,
    'fff'     => 3,
    'abcr'    => 10,
    'adsfefs' => 10
)

So for this i should get output as adsfefs

Sidhant
  • 421
  • 1
  • 6
  • 17

3 Answers3

6

You can use array_keys and pass a second parameter to filter the returned keys to only include the max ones. You can then find the longest key by using array_reduce and a function that checks the lengths of the strings and throws out the shortest one, like so:

$array = array(
    'abc'     => 10,
    'def'     => 8,
    'fff'     => 3,
    'abcr'    => 10,
    'adsfefs' => 10
);

$keys = array_keys($array, max($array));
$longestKey = array_reduce($keys, function ($a, $b) { return strlen($a) > strlen($b) ? $a : $b; });

var_dump($longestKey);

Be aware that if there are two or more strings that are the same length, it will return the last one.

Dan Abrey
  • 696
  • 5
  • 9
  • I realise now that max() works on an array of strings, so the whole two lines could be replaced with simply `$longestKey = max(array_keys($array, max($array)));` – Dan Abrey Jul 23 '18 at 13:49
0
  1. Use arsort(array) to reverse sort them by value
  2. foreach resulting array as long as value is not changing. Store each key in other array in format

    [key] => strlen(key);
    
  3. So arsort(other_array) and take first element. It should be the one with longest keys (but be aware that if there was many keys that has the same length you should add some other conditions to choose between them).

    <?php
    
    $sid = array(
        'abc'     => 10,
        'def'     => 8,
        'fff'     => 3,
        'abcr'    => 10,
        'adsfefs' => 10
    );
    
    arsort($sid);
    
    $prev_val = null;
    $keys = Array();
    
    foreach ($sid as $k=>$v) {
        if ($k < $prev_val) break;
        $keys[$k] = strlen($k);
        $prev_val = $k;
    }
    
    
    arsort($keys);
    
    echo "Longest key with highest value is: " . array_keys($keys)[0] . "\n";
    ?>
    
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31
DevilaN
  • 1,317
  • 10
  • 21
0

Use arsort($data); to perform the initial sort of the value data, then use array_values to identify the largest value. With the largest value find which keys have this value and then iterate through and find the longest key value.

$data = array('bob' => 3, 'rob' => 4, 'nigel' => 6,'john' => 6, 'tony' => 6 );

// Sort array in descending value order
arsort($data);

// Get the first value (will be the largest value)
$maxValue = array_values($data)[0];

// Get an array of all keys with this value
$keys = array_keys($data, $maxValue);

// Find longest key
$maxKey = '';
foreach ($keys as $key) {
    if (strlen($key) > strlen($maxKey)) {
        $maxKey = $key;
    }
}

// Output
print "Largest Value = $maxValue\n";
print "Largest Key with value of $maxValue is $maxKey\n";
Graeme
  • 1,643
  • 15
  • 27