Print out the highest value and its key from an array

Thomas Bengtsson

I'm stuck with an error message that I can't get around: Notice: Array to string conversion. I'm trying to print the highest value and its key of an array.

<?php
$length_array = array();

foreach ($_SERVER as $key => $value) {
    $length = strlen($value);
    $length_array[$key] = $length;
    echo '<pre>'; 
    print_r($key . " = " . $length . " characters"); 
    echo '</pre>';

}

$max_key = array_keys($length_array, max($length_array));
print_r($max_key . " is longest with " . max($length_array) . " characters");
?>

The answer I get is: Array is longest with 444 characters.

How do I get around this?

miken32

array_keys() returns an array. If you expect only one key to have this value you can use array_search() instead:

$server  = array_map("strlen", $_SERVER);
$max     = max($server);
$max_key = array_search($max, $server);

echo "$max_key is longest with $max characters";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

print highest value in dict with key

How to get highest key and its value from a subdocument in mongodb

Print out key values of array from form

How to use max_by to find the parent (and its siblings) key that contains the highest value of a key inside an array?

Print out the highest score

C# - Print out a key and its value in a dictionary depending on a users input?

Getting key with the highest value from object

find highest key with value from Object

Returning the Key with the highest Value from a List of Dictionaries

How can I remove a key and its value from an associative array?

How to get a key from array of objects by its value Javascript

Print the Key for the N-th highest Value in a HashMap

PHP get highest value and lowest value out of array

php array: if identical key values then choose highest by other key value

print out Hash key and value pairs recursively

Print array average and highest

How to print out a specific array and return its pointer from a recursive function

How do we get the most different value from object and print out the key of the value

How do I find the array index with its highest value with vhdl?

Get highest value from multidimensional array

Select highest version value from JSON array

Create an array from an associative array using one of its value in a key value pair

Print array value instead of its name

Distinct Dates with its highest value from an object with javascript

Print smallest value then highest value

How to get a key with highest value from a dictionary that is created from a string?

Get element index of array of objects with the "highest" value for given key

JS: Sort array of objects highest to lowest by key value of the object

I am trying to print the highest odd number but giving my y and z value the highest number (even if its even) creates an issue?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive