Forum Moderators: coopster

Message Too Old, No Replies

How can I get the contents of the largest key of an array of arrays?

         

NeedExpertHelp

6:37 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



Hi,

How can I get the contents of the array key containing the largest amount of data (string) within an array of arrays?

Thanks!

Readie

8:05 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A recursive function involving a lot of foreach and strlen might do it:

[pre]function find_longest_key($input, $out = '') {
foreach($input as $key => $value) {
if(strlen($key) > strlen($out)) {
$out = $key;
}
if(is_array($value)) {
$out = find_longest_key($value, $out);
}
}
return $out;
}[/pre]