note 69460 added to ref.array
| From: | four at fourfivetwofour dot com | Date: | Wed, 06 Sep 2006 21:03:38 +0000 |
| Subject: | note 69460 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116780@lists.php.net to get a copy of this message | ||
Maybe I'm a doofus, or maybe a function like this isn't necessary in the 'real
world', but I needed (and couldn't find) a function to get the KEY of the Longest VALUE in
an Array.
It could probably (definitely?) be done better, but I made this for the time being:
<?php
/*
Get KEY of Longest (string) VALUE in an ARRAY...
*/
function array_get_longest($array)
{
$longest = 0;
for($i=0; $i < count($array); $i++)
{
if (strlen($array[$i]) > strlen($array[$longest]))
{
$longest = $i;
}
}
//return strlen($array[$longest]); // Length (string) of the Longest VALUE.
return $longest; // KEY of Longest (string) VALUE.
}
?>
Please let me know if I've overlooked something, or if this could be done better. Thanks.
----
Server IP: 64.71.164.2
Probable Submitter: 199.82.243.71 (proxied: 199.81.66.74)
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/69460
Del: integrated -- https://master.php.net/note/delete/69460/integrated
Del: useless -- https://master.php.net/note/delete/69460/useless
Del: bad code -- https://master.php.net/note/delete/69460/bad+code
Del: spam -- https://master.php.net/note/delete/69460/spam
Del: non-english -- https://master.php.net/note/delete/69460/non-english
Del: in docs -- https://master.php.net/note/delete/69460/in+docs
Del: other reasons-- https://master.php.net/note/delete/69460
Reject -- https://master.php.net/note/reject/69460
Search -- https://master.php.net/manage/user-notes.php